(Python) Problems with directories that have special characters

  • OS: Windows 03 server
  • Python ver: 2.7

In the code below, its runs are fine when I replace " fuchida@domain.com " with "fuchida". If I use the email format for the directory name, I get the following error: WindowsError: [Error 123] Invalid file name, directory or volume name:. Please let me know what I can do to make this work, my money is on the @ symbol, but I don’t know how to enable it in python.

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() 
+6
source share
1 answer

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

 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() 
+8
source

Source: https://habr.com/ru/post/896393/


All Articles