windows (Python) 具有特殊字符的目录的问题
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/7268618/
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
(Python) Issues with directories that have special characters
提问by Fuchida
- OS: Windows server 03
- Python ver: 2.7
- 操作系统:Windows 服务器 03
- Python 版本:2.7
For the code below, its runs fine when I substitute "[email protected]" with "fuchida". If I use the email format for directory name I get the following error "WindowsError: [Error 123] The filename, directory name, or volume label syntax is incorrect:" . Please let me know what I can do to get this to work, my money is on the "@" symbol fudging things up but I do not know how to resolve it in python so far.
对于下面的代码,当我用“fuchida”替换“[email protected]”时,它运行良好。如果我使用电子邮件格式作为目录名,我会收到以下错误“ WindowsError: [Error 123] 文件名、目录名或卷标语法不正确:”。请让我知道我能做些什么来让它工作,我的钱在“@”符号上,但到目前为止我不知道如何在 python 中解决它。
import os
def dirListing():
dirList = os.listdir("C:\Program Files\home\Server\Logs\[email protected]")
for fname in dirList:
print fname
return
def main():
dirListing()
if __name__ == '__main__':main()
回答by MattH
I suspect problems with your \
as escape characters. Try this:
我怀疑你的\
转义字符有问题。尝试这个:
import os
def dirListing():
dirList = os.listdir(r"C:\Program Files\home\Server\Logs\[email protected]")
for fname in dirList:
print fname
return
def main():
dirListing()
if __name__ == '__main__':main()