Invalid OSError [Errno 22] argument when using open () in 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 use resource files, and something goes wrong when I use it as an argument in an open function, but when I use it to load images and icons, everything is fine.

+10
source share
9 answers

This is the wrong file path. You must either use the full path.

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

Or relative path

open("program_description.txt","r")
+15
source

you must add another "/" in the last "/" path, that is: open('C:\Python34\book.csv')to open('C:\Python34\\book.csv'). For instance:

import csv
with open('C:\Python34\\book.csv', newline='') as csvfile:
    spamreader = csv.reader(csvfile, delimiter='', quotechar='|')
    for row in spamreader:
        print(row)
+4
source

. , !

+2

, open(file_path). , file_path , "?" "<".

+2

"/" :

   open("description_files/program_description.txt","r")
+1
for folder, subs, files in os.walk(unicode(docs_dir, 'utf-8')):
    for filename in files:
        if not filename.startswith('.'):
            file_path = os.path.join(folder, filename)
0

In my case, the problem exists because I did not set the permission for the drive "C: \", and when I change my path to another drive, such as "F: \", my problem is resolved.

0
source

In my case, this problem arose because the result file was opened using "git diff". Maybe this information will help someone.

0
source

On Windows-Pycharm: if File Location | Path contains some string, for example, \tthen you need to escape it using an additional \like\\t

-1
source

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


All Articles