第七色在线视频,2021少妇久久久久久久久久,亚洲欧洲精品成人久久av18,亚洲国产精品特色大片观看完整版,孙宇晨将参加特朗普的晚宴

為了賬號安全,請及時綁定郵箱和手機(jī)立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

Python open 不會根據(jù)文檔創(chuàng)建文件

Python open 不會根據(jù)文檔創(chuàng)建文件

拉風(fēng)的咖菲貓 2022-10-05 16:58:50
我很難相信 python 在 2020 年仍然存在創(chuàng)建文件的問題......所以,我假設(shè)它是 windows 或我......這是帶有輸出的代碼。我嘗試了 w、w+、wt 模式,錯誤消息如下。使用 w, w+, wt 的全部意義在于如果文件不存在則創(chuàng)建該文件我已更改為使用 Path.touch,然后使用僅給出和錯誤輸入的 a、a+ 模式import sysprint("Python version:")print (sys.version)import platformprint("Platform: {} - {} - {}".format(platform.system(), platform.release(), platform.version()))from pathlib import Pathoutput = 'hello world!'save_dir = Path.cwd().joinpath("./sw_out")if(save_dir.exists() == False):    save_dir.mkdir()    print("dir exists now: {}".format(save_dir.exists()))debug_file = save_dir.joinpath("my_debug.txt")#debug_file.touch(exist_ok=True)print(str(debug_file))with(open(debug_file), 'w+') as f:    f.write(output)f.close()Python version:3.7.6 | packaged by conda-forge | (default, Dec 26 2019, 23:30:11) [MSC v.1916 64 bit (AMD64)]Platform: Windows - 10 - 10.0.17763dir exists now: TrueC:\users\me\project\sw_out\my_debug.txt---------------------------------------------------------------------------FileNotFoundError                         Traceback (most recent call last)<ipython-input-19-447d2a994262> in <module>     16 #debug_file.touch(exist_ok=True)     17 print(str(debug_file))---> 18 with(open(str(debug_file)), 'w+') as f:     19     f.write(output)     20 f.close()FileNotFoundError: [Errno 2] No such file or directory: 'C:\\users\\me\\project\\sw_out\\my_debug.txt'
查看完整描述

3 回答

?
繁花如伊

TA貢獻(xiàn)2012條經(jīng)驗 獲得超12個贊

你這樣做是錯的。

改變這個:

with(open(debug_file), 'w+') as f:

至:

with(open(debug_file, 'w+')) as f:

這與以下內(nèi)容相同:

with open(debug_file, 'w+') as f:


查看完整回答
反對 回復(fù) 2022-10-05
?
慕標(biāo)琳琳

TA貢獻(xiàn)1830條經(jīng)驗 獲得超9個贊

簡單的改變應(yīng)該能讓這個工作:


with open(debug_file, 'w+') as f:

    f.write(output)

f.close()


查看完整回答
反對 回復(fù) 2022-10-05
?
德瑪西亞99

TA貢獻(xiàn)1770條經(jīng)驗 獲得超3個贊

試試這樣:


#debug_file.touch(exist_ok=True)

print(str(debug_file))

with open(debug_file, 'w+') as f:

    f.write(output)

f.close()

這對我有用。


查看完整回答
反對 回復(fù) 2022-10-05
  • 3 回答
  • 0 關(guān)注
  • 141 瀏覽
慕課專欄
更多

添加回答

舉報

0/150
提交
取消
微信客服

購課補(bǔ)貼
聯(lián)系客服咨詢優(yōu)惠詳情

幫助反饋 APP下載

慕課網(wǎng)APP
您的移動學(xué)習(xí)伙伴

公眾號

掃描二維碼
關(guān)注慕課網(wǎng)微信公眾號