No os.copy()? Why not?

Steven D'Aprano steve+comp.lang.python at pearwood.info
Wed Apr 4 01:53:44 EDT 2012


On Tue, 03 Apr 2012 15:46:31 -0400, D'Arcy Cain wrote:

> On 03/28/12 16:12, John Ladasky wrote:
>> I'm looking for a Python (2.7) equivalent to the Unix "cp" command.
>> Since the equivalents of "rm" and "mkdir" are in the os module, I
>> figured I look there.  I haven't found anything in the documentation. I
>> am also looking through the Python source code in os.py and its child,
>> posixfile.py.
> 
> cp is not a system command, it's a shell command.  Why not just use the
> incredibly simple and portable
> 
>    >>>open("outfile", "w").write(open("infile").read())
> 
> put it into a method if you find that too much to type:
> 
> def cp(infile, outfile):
>    open(outfile, "w").write(open(infile).read())


Because your cp doesn't copy the FILE, it copies the file's CONTENTS, 
which are not the same thing.

Consider:

* permissions
* access times
* file ownership
* other metadata
* alternate streams and/or resource fork, on platforms that support them
* sparse files


By the time you finish supporting the concept of copying the file itself, 
rather than merely its content, you will have something similar to the 
shutil.copy command -- only less tested.



-- 
Steven



More information about the Python-list mailing list