1

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 ?

2
  • @Cal-cium Sadly, no.. Thank you for trying anyway Commented Dec 9, 2021 at 14:09
  • I used this answer Commented Dec 9, 2021 at 14:22

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

2 Comments

Thanks, but it is not working to.. same issue. The output is the same as when I use os.system(r"G:\Folder\2. Cntry\ACCD.accdb")
I used this answer
-1

for your prompt, try to open change the path like this because the space causes the pb.

G:\Folder\"2. Cntry"\ACCD.accdb

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.