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
115k18 gold badges168 silver badges221 bronze badges
asked Aug 30, 2014 at 15:45
3
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"]
answered Aug 30, 2014 at 15:47
Cory KramerCory Kramer
115k18 gold badges168 silver badges221 bronze badges
2
Add 'r' in starting of path:
path = r"D:\Folder\file.txt"
That works for me.
סטנלי גרונן
2,93724 gold badges46 silver badges68 bronze badges
answered Mar 18, 2020 at 7:47
shailushailu
2904 silver badges10 bronze badges
2
I also ran into this fault when I used
open[r"C:\description_files\program_description.txt","r"]
5. My reason for this fault was that my
open[r"C:\description_files\program_description.txt","r"]
6 had a special character like
open[r"C:\description_files\program_description.txt","r"]
7 or
open[r"C:\description_files\program_description.txt","r"]
8.
tuomastik
4,6595 gold badges38 silver badges49 bronze badges
answered Jul 20, 2018 at 16:20
wolfogwolfog
7317 silver badges10 bronze badges
1
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!
answered Jul 13, 2018 at 17:57
duhaimeduhaime
26k17 gold badges178 silver badges232 bronze badges
For me this issue was caused by trying to write a
open[r"C:\description_files\program_description.txt","r"]
9 to file.
Note: this doesn't work:
open["program_description.txt","r"]
0
The datetime.now[] object contains the colon ''':''' character
To fix this, use a filename which avoid restricted special characters. Note this resource on detecting and replacing invalid characters: //stackoverflow.com/a/13593932/9053474
For completeness, replace unwanted characters with the following:
open["program_description.txt","r"]
1
open["program_description.txt","r"]
2
Note these are Windows restricted characters and invalid characters differ by platform.
answered Dec 1, 2022 at 18:58
Jacob WatersJacob Waters
3071 gold badge3 silver badges11 bronze badges
In my case, I was using an invalid string prefix.
Wrong:
path = f"D:\Folder\file.txt"
Right:
path = r"D:\Folder\file.txt"
answered Mar 6, 2020 at 11:55
RafsRafs
6638 silver badges20 bronze badges
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.
answered Sep 14, 2020 at 14:49
Nick GreenNick Green
1111 silver badge9 bronze badges
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", "-"]
answered Nov 1, 2019 at 15:03
you should add one more "/" in the last "/" of path, that is:
open["program_description.txt","r"]
3 to
open["program_description.txt","r"]
4. 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]
answered Jan 5, 2016 at 15:58
Hiep TranHiep Tran
3,8651 gold badge22 silver badges30 bronze badges
3
Just replace with "/" for file path :
open["description_files/program_description.txt","r"]
answered May 24, 2018 at 7:21
P113305A009D8MP113305A009D8M
3541 gold badge4 silver badges13 bronze badges
1
In Windows-Pycharm: If File Location|Path contains any string like
open["program_description.txt","r"]
5 then need to escape that with additional
open["program_description.txt","r"]
6 like
open["program_description.txt","r"]
7
stderr
8,6171 gold badge35 silver badges50 bronze badges
answered Apr 18, 2018 at 4:35
AbhijeetAbhijeet
8,5715 gold badges71 silver badges77 bronze badges
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
121k44 gold badges238 silver badges297 bronze badges
answered Mar 24, 2021 at 17:24
1
I had special characters like '' in my strings, for example for one location I had a file
open["program_description.txt","r"]
8, then when I tried to save ['Varzea.csv'] with f-string Windows complained. I just "sanitized" the string and all got back to normal.
The best way in my case was to let the strings with just letters, without special characters!
answered Oct 22, 2022 at 1:24
open[r"C:\description_files\program_description.txt","r"]
0
answered Jan 11, 2019 at 23:38
ProstakProstak
3,6257 gold badges36 silver badges47 bronze badges
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.
answered Jan 14, 2019 at 12:25
Mohsen HrtMohsen Hrt
2733 silver badges9 bronze badges
open[r"C:\description_files\program_description.txt","r"]
1
answered Mar 3, 2020 at 5:20
2
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.
answered Nov 18, 2020 at 3:35
When I copy the path by right clicking the file-> properties>security, it shows the error. The working method for this is to copy path and filename separately.
answered Oct 6, 2022 at 0:48
I had faced same issue while working with pandas and trying to open a big csv file:
open[r"C:\description_files\program_description.txt","r"]
2
answered Dec 30, 2022 at 10:08
1
I had the same problem
My fault was:
open["program_description.txt","r"]
9
i tried to open the file as
path = r"D:\Folder\file.txt"
0 containing
path = r"D:\Folder\file.txt"
0 in filename
caution using
path = r"D:\Folder\file.txt"
2 and
path = r"D:\Folder\file.txt"
3 when opening a file
answered Jun 11 at 11:38
0
I faced the same issue, I have resolved it by creating a function that converts backslashes to forward slashes. Probably, this may not be the best way but it fixed my issue.
open[r"C:\description_files\program_description.txt","r"]
3
Then Continue with rest of your code
answered Jul 7 at 0:14
ArunCArunC
11 bronze badge
Date and time in filename
I was trying to create a file with datetime inside, effectively a .txt named i.e - 2023-09-19 12:13:01.txt to create a reports file to store problems/results. As others mentioned before the issue is the special signs in the path. Its not a problem on my mac os but I got this error on win10.
Solution:
open[r"C:\description_files\program_description.txt","r"]
4
Formatted filename would look like this 2023-09-19 12-13-01.txt. You can edit the formatting to whatever works best for you and doesn't error.
answered Sep 19 at 10:53
In my case, the problem was related to lack of permissions between users in my Windows system. I solve this by executing the IDE as administrator. Then, I granted permissions to all users and the problem went away.