path=r"c:\a.txt"
s1="""床前明月光
疑是地上霜
举头望明月
低头思故乡
"""
**#写文件**
#方法1
with open(path,"w") as fp:
fp.write(s1)
#方法2
fp=open(path,"w")
fp.write(s1)
fp.close
**#读文件**
#方法1
#一次读取全部内容
with open(path,"r") as f:
print(f.read())
#方法2
逐行读取文件内容
with open(path,"r") as f:
for i in f:
print(i)
Python写文件到指定路径以及读取文件内容
最新推荐文章于 2024-07-25 03:57:37 发布