代碼試驗:
def save_data(data):
name = f"{dt.now().strftime('%Y_%M_%D_%H_%M')}.txt"
with open(name, 'w') as file:
file.write(data)
我有這個功能應該將抓取的資料保存到 txt 檔案中,但我不斷收到:
FileNotFoundError: [Errno 2] No such file or directory: '2022_06_02/10/22_17_06.txt'
有沒有辦法解決這個問題?
uj5u.com熱心網友回復:
此錯誤訊息...
FileNotFoundError: [Errno 2] No such file or directory: '2022_06_02/10/22_17_06.txt'
...表示您的系統無法找到該檔案2022_06_02/10/22_17_06.txt。
這行代碼:
from datetime import datetime as dt
name = f"{dt.now().strftime('%Y_%M_%D_%H_%M')}.txt"
確實name使用值創建變數,2022_06_02/10/22_17_06.txt但至少我的本地主機是窗戶10based 不允許按名稱創建任何檔案,2022_06_02/10/22_17_06.txt因為它是無效的檔案名。
快照:
![FileNotFoundError: [Errno 2] No such file or directory: '2022_06_02/10/22_17_06.txt' 將抓取的資料寫入 txt 檔案時出錯](https://img.uj5u.com/2022/02/17/6832055b713f40799f3630fb6f2b517a.png)
轉載請註明出處,本文鏈接:https://www.uj5u.com/shujuku/424671.html
標籤:Python 硒 约会时间 硒网络驱动程序 蟒蛇日期时间
上一篇:是否在Selenium4中洗掉了driver.getContextHandles()或driver.context(),因為這些方法即將用于Appium驅動程式?
下一篇:執行緒“main”java.lang.Error中的例外:未解決的編譯問題:執行SeleniumJava程式的令牌“無效字符”上的語法錯誤
