Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

(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 "[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.

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()
like image 822
Fuchida Avatar asked Sep 01 '11 09:09

Fuchida


People also ask

Does Python allow special characters?

Escape sequences allow you to include special characters in strings. To do this, simply add a backslash ( \ ) before the character you want to escape.

How do you mention special characters in Python?

To insert characters that are illegal in a string, use an escape character. An escape character is a backslash \ followed by the character you want to insert.

Is Dash a special character in Python?

You can't have hyphens in variable names in python. That's the subtraction operator. Consider using an underscore instead: education_numType = ...


1 Answers

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()
like image 165
MattH Avatar answered Nov 09 '22 04:11

MattH