zoukankan      html  css  js  c++  java
  • 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文件内容一致,即拷贝成功。

  • 相关阅读:
    为什么非全站升级HTTPS不可?
    使用JSON实现分页
    Git常用命令
    chrome浏览器tab页内存占用变大,网站变慢为哪般?
    我们前端跟后端是怎么合作的
    关于响应式布局
    AngularJS(1)
    关于php语言的使用!
    你必须收藏的Github技巧
    CSS3 动画
  • 原文地址:https://www.cnblogs.com/shannon-V/p/9524528.html
Copyright © 2011-2022 走看看