"with open as f"打不开,甚至没有说有问题

  • 本文关键字:有问题 open with as python
  • 更新时间 :
  • 英文 :

import requests
res = requests.get("https://google.com")
res.raise_for_status()
print(len(res.text))
print(res.text)
with open('mygoogle.html',"w",encoding=('utf-8')) as f :
f.write(res.text)

with open as f部分不工作。如果有,它必须打开新文件,但它没有。

我已经测试了这段代码,它工作正常-mygoogle.html得到编写并包含预期的内容。问题可能在剧本之外。由于它使用的是相对路径,问题可能是文件被写在你不期望的地方,因为工作目录没有设置你期望的方式- Windows通常默认为C:Windowssystem32或你的用户配置文件目录(%userprofile%akaC:users<username>)为新的cmd实例。

最新更新