Running python batch file that has a path with SPACE character

11,979

Solution 1

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"

Solution 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.

Solution 3

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.

Share:
11,979
prosseek
Author by

prosseek

A software engineer/programmer/researcher/professor who loves everything about software building. Programming Language: C/C++, D, Java/Groovy/Scala, C#, Objective-C, Python, Ruby, Lisp, Prolog, SQL, Smalltalk, Haskell, F#, OCaml, Erlang/Elixir, Forth, Rebol/Red Programming Tools and environments: Emacs, Eclipse, TextMate, JVM, .NET Programming Methodology: Refactoring, Design Patterns, Agile, eXtreme Computer Science: Algorithm, Compiler, Artificial Intelligence

Updated on June 29, 2022

Comments

  • prosseek
    prosseek almost 2 years

    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.

  • AndiDog
    AndiDog about 14 years
    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].
  • prosseek
    prosseek about 14 years
    @voyager - It's not my fault, I have no choice but to use the silly directory from the testing system. :(
  • dash-tom-bang
    dash-tom-bang about 14 years
    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.