zoukankan      html  css  js  c++  java
  • 笨办法学python 文本复制

    本来面目

    from sys import argv
    from os.path import exists
    
    script, from_file, to_file = argv
    
    print(f"Copying from {from_file} to {to_file}")
    
    #we could do these two on noe line, how?
    in_file =open(from_file)
    indata = in_file.read()
    
    print(f"The input file is {len(indata)} bytes long")
    print("Ready, hit the RETURN to countinue, CTRL_c to abort")
    input()
    
    out_file = open(to_file,'w')
    out_file.write(indata)
    
    print("Alright, all done.")
    
    out_file.close()
    in_file.close()
    

    第一次简化

    in_file =open(from_file)
    indata = in_file.read()

    合为 indata = open(from_file).read()

    from sys import argv
    from os.path import exists
    
    script, from_file, to_file = argv
    
    print(f"Copying from {from_file} to {to_file}")
    
    #we could do these two on noe line, how?
    indata = open(from_file).read()
    
    print(f"The input file is {len(indata)} bytes long")
    print("Ready, hit the RETURN to countinue, CTRL_c to abort")
    input()
    
    out_file = open(to_file,'w')
    out_file.write(indata)
    #open(to_file,'w').write(open(from_file).read())
    print("Alright, all done.")
    
    out_file.close()
    in_file.close()
    
    

    会报错,因为写了 indata = open(from_file).read() 就无序写关闭语句in_file.close()
    以为其中的read一旦运行,文件就会被Python关闭掉

    from sys import argv
    from os.path import exists
    
    script, from_file, to_file = argv
    
    print(f"Copying from {from_file} to {to_file}")
    
    #we could do these two on noe line, how?
    indata = open(from_file).read()
    
    print(f"The input file is {len(indata)} bytes long")
    print("Ready, hit the RETURN to countinue, CTRL_c to abort")
    input()
    
    out_file = open(to_file,'w')
    out_file.write(indata)
    #open(to_file,'w').write(open(from_file).read())
    print("Alright, all done.")
    
    out_file.close()
    
    

    改成一句话

    
    from sys import argv
    from os.path import exists
    
    script, from_file, to_file = argv
    
    open(to_file,'w').write(open(from_file).read())
    

  • 相关阅读:
    头信息已输出的报错信息位置定位
    阅读<php程序设计>笔记
    include、ruquire使用相对路径总结
    php中未定义的变量使用技巧
    Oracle官方教材(9i、10G及App 11i)
    Vista的软件兼容性
    轻松找回Vista序列号
    oralce定时执行存储过程任务设置步骤详细
    今天做了内存测试,发现真的是内存问题导致的一连串的问题
    网络邮盘(GMailStore) V3.0.2
  • 原文地址:https://www.cnblogs.com/junkdog/p/10125237.html
Copyright © 2011-2022 走看看