Reputation: 39
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'
Upvotes: 3
Views: 13487
Reputation: 71
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).
Upvotes: 7
Reputation: 1991
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
Upvotes: 0
Reputation: 82
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()
Upvotes: 0