windows 运行带有空格字符的路径的python批处理文件

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2594190/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 14:14:33  来源:igfitidea点击:

Running python batch file that has a path with SPACE character

pythonwindowspathbatch-file

提问by prosseek

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

批处理文件是这样的,我将python放在某个路径中具有空格字符的目录中。

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.

方括号 ('[]') 是问题的原因吗?在 Mac 上,python 可以很好地处理括号字符。

回答by AndiDog

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

Windows 控制台中没有反斜杠转义,它应该是

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

回答by Joe L.

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.

看起来你几乎拥有它;只需用单斜线替换双斜线。
如果您不确定并需要正确格式的提醒。尝试使用 Tab 键自动完成您的文件夹或文件名。例如,在您的命令提示符下键入:
C:\docu
然后按 Tab 键,它会自动完成您的文件夹名称。从那里你只需输入其余的。每当您想自动完成文件夹或文件名的其余部分时,只需按 Tab 键即可。这将始终生成可运行的正确指定的路径。

回答by billmanH

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

关闭r"string"中的路径也很好的解决了这个问题。这告诉python它正是那个字符串并避免了反斜杠和空格错误。我将它用于我的文件,因为它允许我直接从窗口顶部的导航栏中复制和粘贴。

回答by Esteban Küber

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.

为什么你有这样一个奇怪的,非描述性的目录名([10,20])?甚至不知道 NTFS 允许这样做。