24
votes
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.

15
For someone else that gets a similar error, you may have invalid characters (for example : or ?) in the filename. - George Ogden
Another scenario (that I just ran into) is if you are trying to write to a file inside a DropBox folder and you just had that file open very recently, you can run into this same error caused by DropBox has detected the changes and is attempting to process your new file. - royce3

15 Answers

31
votes

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")
7
votes

Add 'r' in starting of path:

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

That works for me.

6
votes

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!

6
votes

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

3
votes

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

Wrong:

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

Right:

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

you should add one more "/" in the last "/" of path, that is: open('C:\Python34\book.csv') to open('C:\Python34\\book.csv'). For example:

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

Just replace with "/" for file path :

   open("description_files/program_description.txt","r")
2
votes

In Windows-Pycharm: If File Location|Path contains any string like \t then need to escape that with additional \ like \\t

2
votes

I had the same problem It happens because files can't contain special characters like ":", "?", ">" and etc. You should replace these files by using replace() function:

filename = filename.replace("special character to replace", "-")
1
votes

In my case the error was due to lack of permissions to the folder path. I entered and saved the credentials and the issue was solved.

0
votes
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
votes

In my case,the problem exists beacause I have not set permission for drive "C:\" and when I change my path to other drive like "F:\" my problem resolved.

0
votes
import pandas as pd
df = pd.read_excel ('C:/Users/yourlogin/new folder/file.xlsx')
print (df)
0
votes

I got this error because old server instance was running and using log file, hence new instance was not able to write to log file. Post deleting log file this issue got resolved.

0
votes

just use single quotation marks only and use 'r' raw string upfront and a single '/'

for eg

f = open(r'C:/Desktop/file.txt','r')
print(f.read())