环境

环境
个人电脑 窗户10
python3.11

创建和写入文件

对于开闭

Python3.11
import os
os.getcdir()  # カレントディレクトリを確認(オプション)

c = C:/Users/~/codenote.txt  # 指定のパスを用意

s = 'start'  # 書き込む内容

f = open(c, 'w')  # wで書き込み可能にする
    f.write(s)  # startと書き込む
f.close()

不得不用open-close的集合写很麻烦
使用 with 时关闭是可选的

在与的情况下

with open(c, 'w') as f:
    f.write(s)

with open(c) as f:
    print(f.read())  # 書き込んだ内容確認

参考:https://note.nkmk.me/python-file-io-open-with/


原创声明:本文系作者授权爱码网发表,未经许可,不得转载;

原文地址:https://www.likecs.com/show-308633158.html

相关文章:

  • 2022-12-23
  • 2022-12-23
  • 2021-05-25
  • 2021-12-06
  • 2022-12-23
  • 2021-11-12
猜你喜欢
  • 2021-10-14
  • 2021-12-05
  • 2022-12-23
  • 2022-12-23
  • 2021-12-24
  • 2021-07-20
相关资源
相似解决方案