Concatenate path and filename

Question:

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

filename= "myfile.odt"

subprocess.call(['C:Program Files (x86)LibreOffice 5programsoffice.exe',
                    '--headless',
                    '--convert-to',
                    'pdf', '--outdir',
                    r'C:UsersADesktopRepo',
                    r'C:UsersADesktopRepo'+filename])

But I get this error

SyntaxError: EOL while scanning string literal.

Asked By: nina_berlini

||

Answers:

Try:

import os
os.path.join('C:UsersADesktopRepo', filename)

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

Answered By: zanseb

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:UsersADesktopRepo\'+filename

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

os.path.join(r'C:UsersADesktopRepo', filename)
Answered By: Simon Callan

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:UsersADesktopRepo', filename)
Answered By: Aaron

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 5programsoffice.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 5programsoffice.exe'

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

  • r'' – this is a syntax error

So, this is also wrong:

r'C:UsersADesktopRepo'

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])
Answered By: zvone

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 5program')
# elegant path concatenation
myfile = base_dir / "myfile.odt"

print(myfile)
>>> C:Program Files (x86)LibreOffice 5programmyfile.odt
Answered By: Armin

add library to code :

from pathlib import Path

when u want get current path without filename use this method :

print("Directory Path:", Path().absolute())

now you just need to add the file name to it :for example

   mylink = str(Path().absolute())+"/"+"filename.etc" #str(Path().absolute())+"/"+"hello.txt"

If normally addes to the first path "r" character
for example: r"c://…"

You do not need to do here

Answered By: mamal

You can also simply add the strings together. Personally I like this more.

filename = r"{}{}{}".format(dir, foldername, filename)
Answered By: Ethan
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.