发布时间:2017-11-12 16:05:51编辑:Run阅读(5077)
脚本很短,不过它会让你对于文件操作有更多的了解
代码如下
# coding: utf-8
__author__ = 'www.py3study.com'
from sys import argv
from os.path import exists
script, from_file, to_file = argv
print("Copying from {} to {}".format(from_file, to_file))
in_file = open(from_file)
indata = in_file.read()
print("The input file is {} bytes long".format(len(indata)))
print("Does the output file exist ? {}".format(exists(to_file)))
print("Ready, hit return to continue, CTRL-C to about.")
input()
out_file = open(to_file, 'w')
out_file.write(indata)
print("Alright, all done.")
out_file.close()
in_file.close()import了又一个很好用的命令exists,这个命令将文件名字符串作为参数,如果文件存在的话,它将返回True,否则将返回False
通过使用import,你可以在自己代码中直接使用其它更厉害的程序员写的大量免费代码,这样你就不需要重写一遍了
应该看到的结果

常见问题
为什么'w'要放括号中?
因为这是一个字符串,表示写的意思
len()函数的功能是什么?
它会以数字的形式返回你传递的字符串长度
上一篇: 习题16:读写文件
下一篇: 习题18:命名,变量,代码,函数
51221
50645
41261
38073
32536
29441
28305
23162
23121
21459
1514°
2232°
1854°
1795°
2090°
1846°
2527°
4243°
4104°
2928°