Concatenate path and filename

49,839

Solution 1

Backslash character (\) has to be escaped in string literals.

  • This is wrong: '\'
  • This is correct: '\\' - this is a string containing one backslash

Therefore, this is wrong:

'C:\Program Files (x86)\LibreOffice 5\program\soffice.exe'

There is a trick!

String literals prefixed by r are meant for easier writing of regular expressions. One of their features is that backslash characters do not have to be escaped. So, this would be OK:

r'C:\Program Files (x86)\LibreOffice 5\program\soffice.exe'

However, that wont work for a string ending in backslash:

  • r'\' - this is a syntax error

So, this is also wrong:

r'C:\Users\A\Desktop\Repo\'

So, I would do the following:

import os
import subprocess


soffice = 'C:\\Program Files (x86)\\LibreOffice 5\\program\\soffice.exe'
outdir = 'C:\\Users\\A\\Desktop\\Repo\\'
full_path = os.path.join(outdir, filename)

subprocess.call([soffice,
                 '--headless',
                 '--convert-to', 'pdf',
                 '--outdir', outdir,
                 full_path])

Solution 2

Try:

import os
os.path.join('C:\Users\A\Desktop\Repo', filename)

The os module contains many useful methods for directory and path manipulation

Solution 3

The problem you have is that your raw string is ending with a single backslash. For reason I don't understand, this is not allowed. You can either double up the slash at the end:

r'C:\Users\A\Desktop\Repo\\'+filename

or use os.path.join(), which is the preferred method:

os.path.join(r'C:\Users\A\Desktop\Repo', filename)

Solution 4

To build on what zanseb said, use the os.path.join, but also \ is an escape character, so your string literal can't end with a \ as it would escape the ending quote.

import os
os.path.join(r'C:\Users\A\Desktop\Repo', filename)

Solution 5

To anyone else stumbling across this question, you can use \ to concatenate a Path object and str.

Use path.Path for paths compatible with both Unix and Windows (you can use it the same way as I've used pathlib.PureWindowsPath).

The only reason I'm using pathlib.PureWindowsPath is that the question asked specifically about Windows paths.

For example:

import pathlib
# PureWindowsPath enforces Windows path style
# for paths that work on both Unix and Windows use path.Path
base_dir = pathlib.PureWindowsPath(r'C:\Program Files (x86)\LibreOffice 5\program')
# elegant path concatenation
myfile = base_dir / "myfile.odt"

print(myfile)
>>> C:\Program Files (x86)\LibreOffice 5\program\myfile.odt
Share:
49,839
nina_berlini
Author by

nina_berlini

Updated on October 10, 2020

Comments

  • nina_berlini
    nina_berlini over 3 years

    I have to build the full path together in python. I tried this:

    filename= "myfile.odt"
    
    subprocess.call(['C:\Program Files (x86)\LibreOffice 5\program\soffice.exe',
                        '--headless',
                        '--convert-to',
                        'pdf', '--outdir',
                        r'C:\Users\A\Desktop\Repo\',
                        r'C:\Users\A\Desktop\Repo\'+filename])
    

    But I get this error

    SyntaxError: EOL while scanning string literal.