python循环io.UnsupportedOperation:不可写

Ste*_*ett -1 python loops

尝试输出到文本文件时出现以下错误:io.UnsupportedOperation: not writable

任何帮助表示赞赏。谢谢

f = open("google-searches.txt", "w+")
for item in results:
    google_searches.write("%s\n" % item)
Run Code Online (Sandbox Code Playgroud)

小智 5

您的问题是您正在写入错误的文件。

你已经打开了f,所以你应该写信f,而不是写给google_searches

f = open("google-searches.txt", "w+")
for item in results:
    f.write("%s\n" % item)
Run Code Online (Sandbox Code Playgroud)