Coastharukun 2016-08-13 09:56 采纳率: 0%
浏览 1158

【Python求教】这段代码不知道哪里错了。

from sys import argv
from os.path import exists

script, from_file, to_file = argv

print "We will copy %s to %s." % (from_file, to_file)

indata = open(from_file).read()

print "The input file is %d bytes long." % len(indata)

print "Does the output file exist? %r" % exists(to_file)
print "Ready, hit enter to continue, CTRL-C to abort."
raw_input()

txt = open(to_file, 'w').write(indata)

print txt.read()

一直显示
AttributeError:'Nonetype'object has no attribute 'read'.

  • 写回答

2条回答 默认 最新

  • kimixuchen 2016-08-13 10:37
    关注
        txt = open(to_file, 'w').write(indata)
    

    这段代码执行完后,txt并不是file对象,而是write(indata)函数所返回的对象,根据python文档

    此函数返回None,所以会产生上述错误。

    评论

报告相同问题?

悬赏问题

  • ¥15 多电路系统共用电源的串扰问题
  • ¥15 slam rangenet++配置
  • ¥15 有没有研究水声通信方面的帮我改俩matlab代码
  • ¥15 对于相关问题的求解与代码
  • ¥15 ubuntu子系统密码忘记
  • ¥15 信号傅里叶变换在matlab上遇到的小问题请求帮助
  • ¥15 保护模式-系统加载-段寄存器
  • ¥15 电脑桌面设定一个区域禁止鼠标操作
  • ¥15 求NPF226060磁芯的详细资料
  • ¥15 使用R语言marginaleffects包进行边际效应图绘制