2

The batch file is something like this, I put the python in some directory that has SPACE character in its path.

C:\"Documents and Settings"\Administrator\Desktop\bracket\python\python 
C:\\"Documents and Settings"\\Administrator\\Desktop\\bracket\\[10,20]\\brackettest.py

When I run this one, I get this error.

C:\Documents and Settings\Administrator\Desktop\bracket\python\python: can't ope
n file 'C:\Documents and Settings\\Administrator\\Desktop\\bracket\\[10,20]\\bra
ckettest.py': [Errno 2] No such file or directory
C:\Documents and Settings\Administrator\Desktop\bracket>

What might be wrong?

Wrapping the path doesn't solve this problem.

"C:\\Documents and Settings\\Administrator\\Desktop\\bracket\\[10,20]\\brackettest.py"

Are the brackets ('[]') cause of the problem? On Mac, python works well with bracket character.

1
  • This question is not about Python at all; it is purely about the Windows command line. Commented Jul 31, 2022 at 5:10

4 Answers 4

5

There's no backslash escaping in the Windows console, it should read

"C:\Documents and Settings\Administrator\Desktop\bracket\python\python" "C:\Documents and Settings\Administrator\Desktop\bracket\[10,20]\brackettest.py"
Sign up to request clarification or add additional context in comments.

2 Comments

This definitely works. Make sure the file really exists. You can use tab completion in the console prompt or dir in the directory [10,20].
You can only use tab completion on [10,20] if the open quote is already there, fwiw. I tried from a relative path, without quotes, and cmd.exe treated the bracketed item like it wasn't even there. "cd c:\test\[10,20]<Tab>" gave me the first item in the c:\test folder stuck on the end of [10,20]. Not entirely useful, but if I put a quote before c: then it worked correctly.
2

Looks like you almost had it; just replace the double-slashes with single slashes.
If you're ever unsure and need a reminder of the right format. Try using the tab key to automatically complete your folder or filename. For example, at your command prompt just type:
C:\docu
then hit the tab key and it will automatically complete your folder name. From there you just type in the rest. Whenever you want to automatically complete the rest of the folder or filename, just hit the tab key. This will always produce a properly specified path that is runnable.

Comments

1
path = r"C:\Users\mememe\Google Drive\Programs\Python\file.csv"

Closing the path in r"string" also solved this problem very well. This tells python that it is exactly that string and avoids backslash and space errors. I use it for my files because it allows me to copy and paste directly from the navigation bar at the top of the window.

Comments

0

Can you

cd "\Documents and Settings"
cd "\Documents and Settings\Administrator\Desktop\bracket\python\python"
cd "\Documents and Settings\Administrator\Desktop\bracket\"
cd "\Documents and Settings\Administrator\Desktop\bracket\[10,20]\"

If all that works, do

cd "\Documents and Settings\Administrator\Desktop\bracket\python\python"
python "\Documents and Settings\Administrator\Desktop\bracket\[10,20]\brackettest.py"

Why do you have such a weird, nondescriptive dirname ([10,20])? Didn't even know that NTFS allowed that.

1 Comment

@voyager - It's not my fault, I have no choice but to use the silly directory from the testing system. :(

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.