简体   繁体   English

如何缩短用于将文件复制到另一个文件的Python脚本?

[英]How can I shorten this Python script for copying a file into another file?

Python newbie here. Python新手在这里。 Just starting to learn. 刚刚开始学习。 I am following "How to Learn Python the Hard Way" and one of the exercises is to shorten a script as much as we can. 我正在遵循“如何艰难地学习Python”,其中的一项练习是尽可能地缩短脚本。 I have hit a sort of road block and I'd appreciate any insight. 我遇到了种种障碍,我将不胜感激。 The code simply takes a file and copies it into another file. 该代码仅获取一个文件并将其复制到另一个文件中。 This is what the code looked like at first. 最初的代码就是这样。

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."
raw_input()

out_file = open(to_file, 'w')
out_file.write(indata)

print "Alright, all done."

out_file.close()
in_file.close()

Now the code looks like this: 现在,代码如下所示:

 from sys import argv; script, from_file, to_file = argv; 

 in_data = open(from_file).read()

 out_file = open(to_file, 'w').write(in_data)

Is it cheating to use a semicolon as a way to keep two lines as one line? 使用分号作为将两行保持为一行的方式是否作弊? I got rid of some of the features because I felt like they were pointless for this particular exercise. 我摆脱了某些功能,因为我觉得它们对这项特定练习毫无意义。 The author said he was able to get the script down to one line and I would appreciate any suggestions for how to do this. 作者说他能够将脚本精简到一行,对于执行此操作的任何建议,我将不胜感激。 The script works this way and I've tried fitting it all onto one or two lines with semicolons but I was wondering if there's a better solution. 该脚本以这种方式工作,我尝试使用分号将它们全部拟合到一两行中,但是我想知道是否有更好的解决方案。 Thanks very much. 非常感谢。

You can use shutil.copyfile or shutil.copyfileobj . 您可以使用shutil.copyfileshutil.copyfileobj

http://docs.python.org/2/library/shutil.html http://docs.python.org/2/library/shutil.html

BTW: BTW:

The purpose of shortening your code is usually to make it easier to understand. 缩短代码的目的通常是使其更易于理解。 While using semicolons to merge several statements in one line doesn't count as cheating, it makes your code less readable. 虽然使用分号在一行中合并多个语句不算作作弊,但会使代码的可读性降低。

Well, one-liner, I suppose: 好吧,我想:

open('outfile','w').write(open('infile').read())

This makes me cringe to write code like this. 这使我不愿编写这样的代码。 In fact, don't ever have bare open file handles, use open as a context manager: 实际上,永远不要有裸露的open文件句柄,而将open用作上下文管理器:

with open('infile') as r, open('outfile','w') as w:
    #do things to r and w here

It's both compact and good coding practice. 它既紧凑又良好的编码习惯。

re: semicolons. 回复:分号。 Beautiful is better than ugly. 美丽胜于丑陋。 Avoid them at all cost, unless you're contributing to some code golf . 不惜一切代价避免使用它们,除非您为某些代码高尔夫做贡献。

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

I am also learning this book as a code beginner. 我还以代码初学者的身份学习这本书。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM