• python学习6——拷贝文件


    一、拷贝文件。

    首先建立一个空的txt文档,命名为11.txt。

    from sys import argv
    from os.path import exists
    script, from_file, to_file = argv
    print("Copying from %s to %s." %(from_file,to_file))
    input= open(from_file)
    indata = input_f.read()
    print("The input files is %d bytes long" % len(indata))
    print("Does the output file exist? %r"%exists(to_file))
    print("Ready, hit RETURN to continue, CTRL_C to abort.")
    input()
    output = open(to_file,'w')
    output.write(indata)
    print("Alright, all done.")
    output.close()
    input.close()

    输出结果:

    E:abc>python 11.py 10.txt 11.txt
    Copying from 10.txt to 11.txt.
    The input files is 95 bytes long
    Does the output file exist? True
    Ready, hit RETURN to continue, CTRL_C to abort.
    Traceback (most recent call last):
    File "11.py", line 10, in <module>
    input()
    TypeError: '_io.TextIOWrapper' object is not callable

    出现错误:_io.TextIOWrapper' object is not callable

    原因:我一直按照《笨办法学python》做练习,但是该书中的raw_input不适用于python3,所以我将raw_input改为input,但是这个脚本第五行已经出现了input,第11行的input就无法执行,所以要将其修改,此处改为input_f

    from sys import argv
    from os.path import exists
    script, from_file, to_file = argv
    print("Copying from %s to %s." %(from_file,to_file))
    input_f = open(from_file)
    indata = input_f.read()
    print("The input files is %d bytes long" % len(indata))
    print("Does the output file exist? %r"%exists(to_file))
    print("Ready, hit RETURN to continue, CTRL_C to abort.")
    input()
    output = open(to_file,'w')
    output.write(indata)
    print("Alright, all done.")
    output.close()
    input_f.close()

    输出结果:

    同时查看11.txt文件内容与10.txt文件内容一致,即拷贝成功。

  • 相关阅读:
    App性能测试工具-PerfDog
    痛并快乐着
    SQLyog连接MySQL的前前后后
    组合模式
    Java并发编程:线程池的使用
    高效能人事的七个习惯
    Spring中Bean的生命周期及其扩展点
    (转)第一次有人把“分布式事务”讲的这么简单明了
    分布式事物
    mybatis学习笔记(2)基本原理
  • 原文地址:https://www.cnblogs.com/shannon-V/p/9524528.html
Copyright © 2020-2023  润新知