hello I am trying to apply the file reading function in python
f = open("C:\\Users\\hamza\\Desktop\\family.txt","r")
print(f.read())
It keeps giving me the error of
Traceback (most recent call last):
File "main.py", line 1, in <module>
f = open("C:\\Users\\hamza\\Desktop\\family.txt","r")
FileNotFoundError: [Errno 2] No such file or directory: 'C:\\Users\\hamza\\Desktop\\family.txt'
please help me to resolve this issue
Thankyou
That is the correct code to read a file. C:\\Users\\hamza\\Desktop\\family.txt" must not exist.
You can try following code also,
path="C:\\Users\\hamza\\Desktop\\family.txt"
f = open(path,"r")
print(f.read())
But your code is also correct.
Please check if file is present on desktop and name of file is correct or not
Related
This is my Python code:
from plugin import Plugin
import logging
import yaml
log = logging.getLogger('discord')
def get_bot_prefix():
with open('HarTex/hartexConfig.yaml', 'r') as prefixReader:
prefixValue = yaml.safe_load(prefixReader)
prefixString = prefixValue['settings']['prefix']
return prefixString
prefix = get_bot_prefix()
However I got an error with the file accessing:
Traceback (most recent call last):
File "C:/Users/85251/Documents/Discord Bots/Python/HarTex/bot.py", line 20, in <module>
from plugins.help import Help
File "C:\Users\85251\Documents\Discord Bots\Python\HarTex\plugins\help.py", line 30, in <module>
prefix = get_bot_prefix()
File "C:\Users\85251\Documents\Discord Bots\Python\HarTex\plugins\help.py", line 22, in get_bot_prefix
with open('HarTex/hartexConfig.yaml', 'r') as prefixReader:
FileNotFoundError: [Errno 2] No such file or directory: 'HarTex/hartexConfig.yaml'
How can I fix it? Or am I completely wrong with the directory?
The script should work if you are calling it from the parent directory of HarTex, maybe you are running it from a different working directory?
You could also try to open the file using the full path, as this is probably easy to check.
Error is very clear.You should use absolute path instead of relative path.
For an example home/Prakash/Desktop/test12/test.yaml
Your code definitely work,Once you will change path like this.
I'm scraping a lot of reviews from a site with Python, for each review I call the "review" function and then open the file and append it to it. It works for a while but then the following error appears me everytime and not it the same review.
OSError: [Errno 22] Invalid argument
I tried json.dumps:
scraped_data = reviews(line)
with open('reviews','a' ) as f:
f.write(json.dumps(scraped_data,f,indent = 4))
but the same error keeps appearing. I also tried json.dump:
scraped_data = reviews(line)
with open('reviews','a' ) as f:
json.dump(scraped_data,f,indent = 4))
and, for some reason, I tried without indent too.
edit: full traceback for json.dumps:
Traceback (most recent call last):
File "s.py", line 202, in <module>
with open('reviews','a' ) as f:
OSError: [Errno 22] Invalid argument: 'reviews'
full traceback for json.dump:
Traceback (most recent call last):
File "s.py", line 203, in <module>
json.dump(scraped_data,f,indent = 4)
OSError: [Errno 22] Invalid argument: 'reviews'
On Windows 10
I noticed the same behavior in my code and I found that I was using Microsoft OneDrive which was causing the same error. The file I was trying to open had its file pointer visible in Windows Explorer but not the contents. Are you using any cloud file sharing service?
(I right clicked the file, selected "Always Keep on this Device", ran the same code again and it worked).
Why don't you open your file as a variable?
f = open("reviews", "a")
f.write(json.dumps(scraped_data,f,indent = 4))
f.close()
try giving it the full path of the file.
make sure you have permission to write in that directory (whatever user the app is running under)
also, if the file does not already exist, it cannot append to it... instead of a try a+
plus sign means if it is not there then create it
I'm trying to read this .txt file through Python (3.6.5), using SublimeText3 (3.1.1). learning_python.txt is in the same directory as my python program. I tried to make it an absolute filepath but getting FileNotFoundError either way. I also tried running it through Terminal with the same outcome. Is the code wrong?
filename = 'learning_python.txt'
print("--- Reading in the entire file:")
with open(filename) as f:
contents = f.read()
print(contents)
Traceback is:
--- Reading in the entire file:
Traceback (most recent call last):
File "C:\Users\sulli\Documents\Coding\Python Crash Course\Ch_10_Files_Exceptions\about_python.py", line 6, in <module>
with open(filename) as f:
FileNotFoundError: [Errno 2] No such file or directory: 'C:/Users/sulli/Documents/Coding/Python Crash Course/Ch_10_Files_Exceptions/learning_python.txt'
Looks like the issue here was I had named learning_python.txt file with the .txt at the end just like it is in the code. When I deleted the .txt on the text file itself, it worked in Python to find learning_python.txt. Python must have seen it as learning_python.txt.txt.
I write a basic file write code -
f = open('workfile', 'r+')
f.write('0123456789abcdef')
I run the file at cmd at the same folder where I put workfile.txt file but I get the error -
IOError: [Errno 2] No such file or directory: 'workfile'
I tried also to convert to exe file with py2exe and run the exe file but nothing...
what is the problem?
thanks!
====================
the full error massage when check on compiler-
Traceback (most recent call last):
File "/home/ubuntu/workspace/.c9/metadata/workspace/2.py", line 1, in <module>
f = open('workfile', 'r+')
IOError: [Errno 2] No such file or directory: 'workfile'
rename 'workfile' to 'workfile.txt':
f = open('workfile.txt', 'r+')
I know you can open files, browsers, and URLs in the Python GUI. However, I don't know how to apply this to programs. For example, none of the below work. (The below are snippets from my growing chat bot program):
def browser():
print('OPENING FIREFOX...')
handle = webbroswer.get() # webbrowser is imported at the top of the file
handle.open('http://youtube.com')
handle.open_new_tab('http://google.com')
and
def file():
file = str(input('ENTER THE FILE\'S NAME AND EXTENSION:'))
action = open(file, 'r')
actionTwo = action.read()
print (actionTwo)
These errors occur, in respect to the above order, but in individual runs:
OPENING FIREFOX...
Traceback (most recent call last):
File "C:/Users/RCOMP/Desktop/Programming/Python Files/AI/COMPUTRON_01.py", line 202, in <module>
askForQuestions()
File "C:/Users/RCOMP/Desktop/Programming/Python Files/AI/COMPUTRON_01.py", line 64, in askForQuestions
browser()
File "C:/Users/RCOMP/Desktop/Programming/Python Files/AI/COMPUTRON_01.py", line 38, in browser
handle = webbroswer.get()
NameError: global name 'webbroswer' is not defined
>>>
ENTER THE FILE'S NAME AND EXTENSION:file.txt
Traceback (most recent call last):
File "C:/Users/RCOMP/Desktop/Programming/Python Files/AI/COMPUTRON_01.py", line 202, in <module>
askForQuestions()
File "C:/Users/RCOMP/Desktop/Programming/Python Files/AI/COMPUTRON_01.py", line 66, in askForQuestions
file()
File "C:/Users/RCOMP/Desktop/Programming/Python Files/AI/COMPUTRON_01.py", line 51, in file
action = open(file, 'r')
IOError: [Errno 2] No such file or directory: 'file.txt'
>>>
Am I handling this wrong, or can I just not use open() and webbrowser in a program?
You should read the errors and try to understand them - they are very helpful in this case - as they often are:
The first one says NameError: global name 'webbroswer' is not defined.
You can see here that webbrowser is spelled wrong in the code. It also tells you the line it finds the error (line 38)
The second one IOError: [Errno 2] No such file or directory: 'file.txt' tells you that you're trying to open a file that doesn't exist. This does not work because you specified
action = open(file, 'r')
which means that you're trying to read a file. Python does not allow reading from a file that does not exist.