Daria Ivanova
Daria Ivanova

Reputation: 167

I`m getting an error "[Errno 22] Invalid argument" while trying to open the file

I am trying to write a function which takes a file and returns a list. But it doesn`t work. Instead it says:

'IOError: [Errno 22] Invalid argument: 'C:\Python32\x07ssignment3\wordlist.txt''

when I am trying to run the module

words_file_name = 'C:\Python32\assignment3\wordlist.txt'
words_file = open(words_file_name, 'r')

def read_words(words_file):
    words_list = words_file.readlines()
    return words_list

Upvotes: 0

Views: 14328

Answers (2)

Anuj Kumar
Anuj Kumar

Reputation: 91

You can avoid this by marking \a in your \assignement as \\a. Whenever you encounter \b, \n, \r, \t etc. in your directory path, Replace them with a double slash.

Upvotes: 0

TigerhawkT3
TigerhawkT3

Reputation: 49318

Escape the backslashes or use a raw string literal. Otherwise, that \a is turning into \x07, which is the hex representation of the escape character \a. I'd recommend using raw strings for this so you don't have to deal with extra backslashes.

words_file_name = r'C:\Python32\assignment3\wordlist.txt'
                  ^

Upvotes: 3

Related Questions