开发者

(Python) Issues with directories that have special characters

  • OS: Windows server 03
  • Python ver: 2.7

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:开发者_如何学Python" . 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()
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜