Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

OSError [Errno 22] invalid argument when use open() in Python

Tags:

python

def choose_option(self):
        if self.option_picker.currentRow() == 0:
            description = open(":/description_files/program_description.txt","r")
            self.information_shower.setText(description.read())
        elif self.option_picker.currentRow() == 1:
            requirements = open(":/description_files/requirements_for_client_data.txt", "r")
            self.information_shower.setText(requirements.read())
        elif self.option_picker.currentRow() == 2:
            menus = open(":/description_files/menus.txt", "r")
            self.information_shower.setText(menus.read())

I am using resource files and something is going wrong when i am using it as argument in open function, but when i am using it for loading of pictures and icons everything is fine.

like image 407
eugene Avatar asked Aug 30 '14 15:08

eugene


People also ask

What(): OSError Errno 22 invalid argument?

The OSError: (errno 22) invalid argument occurs may also occur when one is trying to save a file. For example: adding an unnecessary semicolon to the filename also causes the error. It is because windows do not allow semi-colons in file names. That was it for OSError: (errno 22) invalid argument.

What is Python OSError?

OSError is a built-in exception in Python and serves as the error class for the os module, which is raised when an os specific system function returns a system-related error, including I/O failures such as “file not found” or “disk full”.


5 Answers

Add 'r' in starting of path:

path = r"D:\Folder\file.txt"

That works for me.

like image 25
shailu Avatar answered Oct 09 '22 00:10

shailu


That is not a valid file path. You must either use a full path

open(r"C:\description_files\program_description.txt","r")

Or a relative path

open("program_description.txt","r")
like image 152
Cory Kramer Avatar answered Oct 08 '22 23:10

Cory Kramer


I also ran into this fault when I used open(file_path). My reason for this fault was that my file_path had a special character like "?" or "<".

like image 9
wolfog Avatar answered Oct 08 '22 23:10

wolfog


I received the same error when trying to print an absolutely enormous dictionary. When I attempted to print just the keys of the dictionary, all was well!

like image 6
duhaime Avatar answered Oct 09 '22 01:10

duhaime


In my case, I was using an invalid string prefix.

Wrong:

path = f"D:\Folder\file.txt"

Right:

path = r"D:\Folder\file.txt"
like image 4
RTD Avatar answered Oct 09 '22 01:10

RTD