No os.copy()? Why not?

Alain Ketterlin alain at dpt-info.u-strasbg.fr
Wed Apr 4 05:22:45 EDT 2012


Steven D'Aprano <steve+comp.lang.python at pearwood.info> writes:

> 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.

>>    >>>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.

A minor point, but shutil.copy only "copies" contents and permissions
(no access times, etc.) You probably mean shutil.copy2.

And sparse files are really hard to reproduce, at least on Unix: on
Linux even the system's cp doesn't guarantee sparseness of the copy (the
manual mentions a "crude heuristic").

But of course shutil.copy is the best solution to mimic a raw cp.

-- Alain.



More information about the Python-list mailing list