Trying to open a file using shell :
os.system("G:\Folder\2. Cntry\ACCD.accdb")
throw the following error :
'G:\Folder.' is not recognized as an internal or external command,
operable program or batch file.
However when I paste "G:\Folder\2. Cntry\ACCD.accdb"
into cmd prompt, it does open the file.
It seems that \2
is read as a .
but using :
os.system(r"G:\Folder\2. Cntry\ACCD.accdb")
returns :
'G:\Folder\2.' is not recognized as an internal or external command,
operable program or batch file.
How can I do ?
CodePudding user response:
As the backslash is an escape character in Python, you could:
- Use an raw string:
r"G:\Folder\2. Cntry\ACCD.accdb"
- Use forward slashes:
"G:/Folder/2. Cntry/ACCD.accdb"
- Escape the backslashes:
"G:\\Folder\\2. Cntry\\ACCD.accdb"
Which works for any subprocess
function.
Using os.system
you need to pass the path additionally surrounded with single quotes:
import os
import subprocess
paths = (
[r"C:\Temp\2. Cntry\executer.exe", r'"C:\Temp\2. Cntry\executer.exe"'],
["C:/Temp/2. Cntry/executer.exe", '"C:/Temp/2. Cntry/executer.exe"'],
["C:\\Temp\\2. Cntry\\executer.exe", '"C://Temp//2. Cntry//executer.exe"'],
)
for p1, p2 in paths:
subprocess.call(p1)
os.system(p2)
Out:
Arguments passed:
C:\Temp\2. Cntry\executer.exe
done
Arguments passed:
C:\Temp\2. Cntry\executer.exe
done
Arguments passed:
C://Temp//2. Cntry//executer.exe
done
Arguments passed:
C://Temp//2. Cntry//executer.exe
done
Arguments passed:
C:\Temp\2. Cntry\executer.exe
done
Arguments passed:
C://Temp//2. Cntry//executer.exe
done
CodePudding user response:
for your prompt, try to open change the path like this because the space causes the pb.
G:\Folder\"2. Cntry"\ACCD.accdb