OSError: [Errno 22] Invalid argument: - python

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

Related

While opening the file getting OSError: [Errno 16] Device or resource busy: '/sys/kernel/debug/tracing/uprobe_events' error

Below python script is used to write data in to file
fileHandler = open("/sys/kernel/debug/tracing/uprobe_events", "w")
fileHandler.write("1")
fileHandler.close()
Error Message:
Traceback (most recent call last):
File "File_Operation.py", line 4, in <module>
fileHandler = open("/sys/kernel/debug/tracing/uprobe_events", "w")
OSError: [Errno 16] Device or resource busy: '/sys/kernel/debug/tracing/uprobe_events'
I got the same error. Unfortunately, it is a general error and without a context it is difficult to understand why it happened.
I've looked for solutions, but nothing very useful. You can have a look here, maybe your problem is related to another in this thread:
https://www.linuxquestions.org/questions/linux-embedded-and-single-board-computer-78/what-is-error-ioerror-%5Berrno-16%5D-device-or-resource-busy-4175599788/

Why can I suddenly no longer write to files in Python?

Whenever I try to write or modify the data of a file in any way I get this error everytime:
OSError: [Errno 9] Bad file descriptor
Here's what I've been trying to do:
# output.txt is the file already created inside of the same directory
with open(__file__.rsplit("\\", 1)[0] + "\\output.txt", "w") as f:
f.write("this should write to 'output.txt'")
I would try to dump with json or append with normal data but I'm continuously recieving the same error.
In the above example, here is the entire output of the terminal after execution:
C:\Users\USER\Documents\Programming\Code\Python\Testing>c:\Users\USER\Documents\Programming\Code\Python\Testing\z_3.py
OSError: [Errno 9] Bad file descriptor
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "C:\Users\USER\Documents\Programming\Code\Python\Testing\z_3.py", line 4, in <module>
f.write("this should write to 'output.txt'")
OSError: [Errno 9] Bad file descriptor
This is all very strange because I've been able to write to files normally but I've just recently reinstalled windows and now it won't work.

Reading File function in python

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

Error message when using openpyxl.load_workbook() function

I have just started to learn programming and I am currently trying to read an excel file from IDLE. I'm following instruction from the book "Automate the Boring Stuff". I have successfully imported openpyxl, and thereafter, as instructed tried wb = openpyxl.load_workbook('example.xlsx') where I exchanged "example" to the actual name of the workbook. However, I get this error message:
Traceback (most recent call last): File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 117, in load_workbook
archive = ZipFile(filename, 'r', ZIP_DEFLATED) File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/zipfile.py", line 1216, in __init__
self.fp = io.open(file, filemode) FileNotFoundError: [Errno 2] No such file or directory: 'jan.xlsx'
During handling of the above exception, another exception occurred:
Traceback (most recent call last):
File "<pyshell#4>", line 1, in <module>
wb = openpyxl.load_workbook('jan.xlsx')
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/openpyxl/reader/excel.py", line 145, in load_workbook
raise InvalidFileException(unicode(e))
openpyxl.exceptions.InvalidFileException: [Errno 2] No such file or directory: 'jan.xlsx'
I don't understand how to solve this.
This error message simply says that python can't locate the file. When you try to open a file 'jan.xlsx', it's trying to locate it in the base directory of your code in your IDE. So say your code is in a directory called /Users/username/PycharmProjects/myCode
(I'm assuming here you are on a Mac OS as the path to your python suggests...
but jan.xlsx is in /Users/username
Since that is 2 directories up from your code directory, you can do one of two things:
Write in the absolute path to the file:
wb = openpyxl.load_workbook('/Users/username/jan.xlsx')
Use a relative path that is relative to the base project directory. Two dots in a relative path means one level up from the current directory. So if the excel file is 2 levels up, you can do:
wb = openpyxl.load_workbook('../../jan.xlsx')

fd.seek() IOError: [Errno 22] Invalid argument

My Python Interpreter (v2.6.5) raises the above error in the following codepart:
fd = open("some_filename", "r")
fd.seek(-2, os.SEEK_END) #same happens if you exchange the second arg. w/ 2
data=fd.read(2);
last call is fd.seek()
Traceback (most recent call last):
File "bot.py", line 250, in <module>
fd.seek(iterator, os.SEEK_END);
IOError: [Errno 22] Invalid argument
The strange thing with this is that the exception occurs just when executing my entire code, not if only the specific part with the file opening.
At the runtime of this part of code, the opened file definitely exists, disk is not full, the variable "iterator" contains a correct value like in the first codeblock.
What could be my mistake?
Thanks in advance
From lseek(2):
EINVAL
whence is not one of SEEK_SET,
SEEK_CUR, SEEK_END; or the resulting
file offset would be negative, or
beyond the end of a seekable device.
So double-check the value of iterator.

Categories

Resources