m0_71568769 2022-07-23 17:57 采纳率: 100%
浏览 16
已结题

为什么数据写入的是out_file变量,但结果会出现在to_filr中呢?(语言-python)

#读取数据后的数据写入了out_file变量,是哪一步完成了将数据写入to_file中呢?
#
from sys import argv
from os.path import exists

script, from_file, to_file = argv

print(f"Copping from {from_file} to {to_file}")

#we could do these two on one line, how?
in_file = open(from_file)
in_data = in_file.read()

print(f"The input file is {len(in_data)} bytes long")

print(f"Does the output file exists? {exists(to_file)}")
print("Ready, hit RETURN to continue, CTRL-C to abort.")
input()

out_file = open(to_file, 'w')
out_file.write(in_data)

print("Alright, all done")
in_file.close()
out_file.close()

  • 写回答

2条回答 默认 最新

  • 快乐小土狗 2022-07-23 18:44
    关注

    img


    to_file保存的是一个字符串,这个字符串是一个文件的路径
    out_file = open(to_file, 'w')的意思是以w模式(覆盖写模式)去打开to_file(文件路径)文件。返回一个文件对象。
    out_file.write(in_data),文件对象执行写入操作

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

问题事件

  • 系统已结题 8月9日
  • 已采纳回答 8月1日
  • 创建了问题 7月23日