关于文件的基本命令:
truncate: 清空文件
write('stuff'): 将“stuff”写入文件
seek(0): 将读写位置移动到文件开头
#写入内容到文件
from sys import argv script,filename = argv print(f"找到我们的{filename}文件") print(正在打开文件) target = open(filename,'w'正在清理文件内容) target.truncate() 现在输入写入每一行的内容) line1 = input(line1:) line2 = input(line2:) line3 = input(line3:) 把你的内容写入文本) target.write(line1) target.write(\n) target.write(line2) target.write() target.write(line3) 正在关闭文件) target.close()
#文件复制 from os.path exists out1 = input(输入复制源文件:) out2 = input(输入复制目的文件:if exists(out1): out1_file = open(out1) file_data = out1_file.read() out1_file.close() else: {out1}文件不存在 exists(out2): out2_file = open(out2,1)">) out2_file.write(file_data) out2_file.close() {out2}文件不存在")
一个文件内容复制到另一个文件 from sys argv exists script,from_file,to_file =从{from_file}复制到{to_file}) in_file = open(from_file) indata = in_file.read() 这个文件的的字节长度是{len(indata)}输出文件是否存在:{exists(to_file)}) out_file = open(to_file,1)">) out_file.write(indata) 关闭所有文件) out_file.close() in_file.close()