(Python)具有特殊字符的目录的问题 [英] (Python) Issues with directories that have special characters

查看:293
本文介绍了(Python)具有特殊字符的目录的问题的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述


  • 操作系统:Windows Server 03

  • Python ver:2.7

对于下面的代码,当用fuchida替换fuchida@domain.com时,它的运行正常。如果我使用目录名称的电子邮件格式,我会收到以下错误 WindowsError:[错误123]文件名,目录名或卷标语法不正确:。请让我知道我可以做些什么来让这个工作,我的钱是在@符号上捣蛋的东西,但我不知道如何解决它在python到目前为止。

For the code below, its runs fine when I substitute "fuchida@domain.com" 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.

import os

def dirListing():
    dirList = os.listdir("C:\\Program Files\home\Server\Logs\fuchida@domain.com")
    for fname in dirList:
        print fname
    return

def main():
    dirListing()

if __name__ == '__main__':main()


推荐答案

我怀疑您的 \ 作为转义字符存在问题。尝试这样:

I suspect problems with your \ as escape characters. Try this:

import os

def dirListing():
    dirList = os.listdir(r"C:\\Program Files\home\Server\Logs\fuchida@domain.com")
    for fname in dirList:
        print fname
    return

def main():
    dirListing()

if __name__ == '__main__':main()

这篇关于(Python)具有特殊字符的目录的问题的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆