简体   繁体   English

OSError: [Errno 22] 无效参数:

[英]OSError: [Errno 22] Invalid argument:

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.我正在使用 Python 从一个站点上抓取大量评论,对于每条评论,我都会调用“评论”函数,然后打开文件并将其附加到其中。 It works for a while but then the following error appears me everytime and not it the same review.它工作了一段时间,但每次都会出现以下错误,而不是相同的评论。

OSError: [Errno 22] Invalid argument OSError: [Errno 22] 无效参数

I tried json.dumps:我试过 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:我也试过 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:编辑: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: 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在 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.我在我的代码中注意到了相同的行为,我发现我正在使用导致相同错误的 Microsoft OneDrive。 The file I was trying to open had its file pointer visible in Windows Explorer but not the contents.我试图打开的文件在 Windows 资源管理器中可见其文件指针,但看不到内容。 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). (我右键单击该文件,选择“始终保留在此设备上”,再次运行相同的代码并且它起作用了)。

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+同样,如果文件不存在,它可以不追加给它......而不是a尝试a+

plus sign means if it is not there then create it加号意味着如果它不存在则创建它

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()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM