Copy a file in python

Peter Hansen peter at engcorp.com
Fri Dec 6 08:29:04 EST 2002


Mikael Olofsson wrote:
> 
> On 4 Dec 2002 00:46:29 -0800
> anandpillai6 at yahoo.com (Anand) wrote:
> > What is the function to copy a file in python?
> 
> If I were to be verbose, I would probably do the following
> 
> originalFile = open(originalPath,'r')
> targetFile = open(targetPath,'w')
> targetFile.write(originalfile.read())
> 
> But to be honest, I would rather write this as
> 
> open(targetPath,'w').write(open(originalPath,'r').read())

This is almost unreadable.  Why not try to preserve 
maintainability in your code, by using a couple extra
lines like you did above?  The nice variable names 
make it self-documenting, and the simplicity of each
line makes it easy to understand at a glance.

-Peter



More information about the Python-list mailing list