Concatenate path and filename [duplicate]
Asked Answered
E

7

12

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.

Eonism answered 14/11, 2016 at 19:52 Comment(2)
String literals can't end with a single \, else you'll be escaping the ' which leaves the string openAmary
Also notice the highlighting of the string https://mcmap.net/q/910103/-getting-error-on-if-and-elifAmary
T
7

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])
Tailband answered 14/11, 2016 at 20:2 Comment(0)
O
35

Try:

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

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

Operate answered 14/11, 2016 at 19:55 Comment(1)
It's good to use os joining, but the real issue here is the ' is being escaped.Mckeever
T
7

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])
Tailband answered 14/11, 2016 at 20:2 Comment(0)
P
2

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)
Perturb answered 14/11, 2016 at 20:2 Comment(1)
Well my guess why a trailing backslash is not allowed would be that it escapes the closing quote. I haven't looked this up, but while r'' allows one to write backslashes without escaping, you still need a way to escape a single quote in there, so \' is still an escaped quote and the single-quoted string continues to the next single quote. This is consistent with the syntax error because the interpreter thought it's reading a string when the file ended.Salinger
M
2

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)
Mckeever answered 14/11, 2016 at 20:2 Comment(0)
F
1

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
Femineity answered 4/2, 2020 at 21:27 Comment(1)
1. Your first sentence uses backslash but your code uses forward slash (/). 2. The / operator does not simply concatenate the second argument, it performs a path join. If you actually just want to append a string, / will not work.Multiform
A
1

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

Allembracing answered 8/9, 2020 at 9:28 Comment(0)
L
1

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

filename = r"{}{}{}".format(dir, foldername, filename)
Liverwurst answered 28/11, 2022 at 15:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.