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

为什么数据写入的是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日

悬赏问题

  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)
  • ¥100 set_link_state
  • ¥15 虚幻5 UE美术毛发渲染
  • ¥15 CVRP 图论 物流运输优化
  • ¥15 Tableau online 嵌入ppt失败
  • ¥100 支付宝网页转账系统不识别账号
  • ¥15 基于单片机的靶位控制系统
  • ¥15 真我手机蓝牙传输进度消息被关闭了,怎么打开?(关键词-消息通知)
  • ¥15 装 pytorch 的时候出了好多问题,遇到这种情况怎么处理?