File system object manipulation (learner)

Malcolm Tredinnick malcolm at commsecure.com.au
Wed May 16 10:07:38 EDT 2001


On Wed, May 16, 2001 at 02:17:17PM +0100, Scott Finnie wrote:
> I'm looking to write some fairly noddy tools for synchronising files
> between two systems - as much as a learning exercise as anything else.

If you hand't added the last clause, about a dozen people would be
saying "program ABCD does this already".

> The Python core libs support file operations, but they deal with file
> contents; I need a higher level than that, and don't want to write a
> file copy in Python (too slow).  Shopping list as follows:

Everything you list below is part of the standard distribution's set of
modules (which is awfully convenient for portability). However, since I
don't use Windows, I'm not sure if everything works the same there --
that's an exercise for you to work out. :-)

> . list files in directory (ideally recursively)

Look at os.path.walk() and os.listdir().

> . copy file from one location to another

Look in the shutils module.

> . get file stats (date, time, size, - os.* call for this exists I think)

As you suspected, in the os module (although some are Unix onlyi, so be
careful of portability).

> . compare contents (e.g. generate MD5 checksums and compare?)

There's an md5 module. You may also want to look at the rsync program
(which has nothing to do with Python). That has an interesting algorithm
for comparing files and transferring small differences. However,
understanding the rsync algorithm is (trust me on this) non-trivial.

> . create/explode archives using standard formats (e.g. tar, (g)zip)

There is a gzip module. There is no tar module in the standard distro
(and a quick search in the Vaults of Parnassus reveals nothing of note).
You may need to do some work there.

> . compress/uncompress files (would be nice).

There are both zip and zlib modules.

> I'm a bit unsure where best to turn for available modules: goals are
> ideally cross platform (win32+unix) although win32 is most important.

Download the documentation. The "global module index" section is worth
at least glancing through once, just to see what is available.

Cheers,
Malcolm

-- 
Despite the cost of living, have you noticed how popular it remains?




More information about the Python-list mailing list