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

Cory Kramer
  • 107,498
  • 14
  • 145
  • 201
eugene
  • 319
  • 1
  • 3
  • 3
  • 6
    For someone else that gets a similar error, you may have invalid characters (for example `:` or `?`) in the filename. – George Ogden Sep 10 '20 at 09:26
  • 1
    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 May 29 '21 at 03:37

15 Answers15

43

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")
Cory Kramer
  • 107,498
  • 14
  • 145
  • 201
10

Add 'r' in starting of path:

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

That works for me.

סטנלי גרונן
  • 2,849
  • 23
  • 46
  • 65
shailu
  • 150
  • 1
  • 8
7

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

tuomastik
  • 4,122
  • 5
  • 33
  • 44
wolfog
  • 681
  • 7
  • 8
6

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!

duhaime
  • 23,078
  • 13
  • 147
  • 194
4

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

Wrong:

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

Right:

path = r"D:\Folder\file.txt"
RTD
  • 494
  • 5
  • 17
3

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.

Nick Green
  • 91
  • 9
2

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)
Charlie Joynt
  • 4,092
  • 1
  • 25
  • 44
Hiep Tran
  • 3,004
  • 1
  • 18
  • 21
  • 1
    Welcome to Stack Overflow! Whilst this may theoretically answer the question, [it would be preferable](//meta.stackoverflow.com/q/8259) to include the essential parts of the answer here, and provide the link for reference. – Enamul Hassan Jan 05 '16 at 16:14
  • 6
    There's a set of problems with this answer: 1) `open('C:\Python34\book.csv')` to `open('C:\Python34\book.csv')` -> There is no difference between the two lines. Did you mean `open('C:\Python34\book.csv')` to `open('C:\\Python34\\book.csv')`? 2) Both the code in the example and in the first line is wrong, as you're not escaping backslashes (or, in the case of your example, all of them.) `'C:\Python34\\book.csv'` should be `'C:\\Python34\\book.csv'` or `r'C:\Python34\book.csv'` – GPhilo May 31 '17 at 09:41
  • 1
    If you click Edit you'll see that @hiep-tran did type double slashes, but SO rendered these as single slashes. I fixed the formatting with backticks around the example commands. – Charlie Joynt Mar 15 '18 at 10:59
2

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

stderr
  • 8,331
  • 1
  • 33
  • 50
Abhijeet
  • 8,114
  • 5
  • 68
  • 74
2

Just replace with "/" for file path :

   open("description_files/program_description.txt","r")
P113305A009D8M
  • 274
  • 1
  • 4
  • 11
2

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", "-")
Lio Ak
  • 21
  • 2
2

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())
Yu Hao
  • 115,525
  • 42
  • 225
  • 281
0
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)
Prostak
  • 3,225
  • 7
  • 33
  • 45
0

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.

Mohsen Hrt
  • 227
  • 2
  • 7
0
import pandas as pd
df = pd.read_excel ('C:/Users/yourlogin/new folder/file.xlsx')
print (df)
I Iv
  • 21
  • 1
0

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.

Abhishek Gaur
  • 587
  • 7
  • 11