(Python) Issues with directories that have special characters

被刻印的时光 ゝ 提交于 2020-01-23 06:17:29

问题


  • 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:" . 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()

回答1:


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()


来源:https://stackoverflow.com/questions/7268618/python-issues-with-directories-that-have-special-characters

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!