#文件操作-修改占硬盘
import os
name = '库里'
new_name = '咖喱仔'
f_name = '文件操作'
f_new_name = '%s.new'%f_name
f = open(f_name,'r')
f_new = open(f_new_name,'w')
for line in f:
if name in line:
line = line.replace(name,new_name)
f_new.write(line)
f.close()
f_new.close()
# os.rename(f_new_name,f_name)这个方法会报错,好像是跨平台的问题
os.replace(f_new_name,f_name)
python全栈笔记-day05-file_operation
最新推荐文章于 2022-01-08 15:56:14 发布