Reputation: 195
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.
Upvotes: 12
Views: 84564
Reputation: 171
You can also simply add the strings together. Personally I like this more.
filename = r"{}{}{}".format(dir, foldername, filename)
Upvotes: 1
Reputation: 1986
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
Upvotes: 1
Reputation: 168
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
Upvotes: 1
Reputation: 1425
Try:
import os
os.path.join('C:\Users\A\Desktop\Repo', filename)
The os module contains many useful methods for directory and path manipulation
Upvotes: 38
Reputation: 19352
Backslash character (\
) has to be escaped in string literals.
'\'
'\\'
- this is a string containing one backslashTherefore, this is wrong:
'C:\Program Files (x86)\LibreOffice 5\program\soffice.exe'
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 errorSo, 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])
Upvotes: 7
Reputation: 2073
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)
Upvotes: 2
Reputation: 3130
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)
Upvotes: 2