Python 3.x 如何缩短这些关于将一个文本文件的内容复制到另一个文本文件的代码?
代码来自在线python书籍《艰难学习python》,第3版.pdf 我想知道缩短代码的其他方法,以使其能够提供相同的输出Python 3.x 如何缩短这些关于将一个文本文件的内容复制到另一个文本文件的代码?,python-3.x,Python 3.x,代码来自在线python书籍《艰难学习python》,第3版.pdf 我想知道缩短代码的其他方法,以使其能够提供相同的输出 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)) in_file = open(from_file) indata = in_file.read(
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))
in_file = open(from_file)
indata = in_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 RETURN to continue, 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()
只需使用shutil库中的copyfile即可。为什么需要更短?还要注意的是,它不仅不需要更短,而且也不符合PEP-8指南。不要在函数和它们的参数之间留空格,也不要留太多的空行。这只是为了练习,无论如何,谢谢。