reading and removing first x bytes of a file

Peter Hansen peter at engcorp.com
Thu Apr 29 10:06:54 EDT 2004


bart_nessux wrote:

> I have some Macbinary files on a PC. I want to recursively read these 
> files and remove the first 128 bytes of the files if they contain the 
> macbinary header info. I know how to read directories recursively, but 
> how would I read the first 128 bytes of each file in the path?

os.listdir() can return a list of the names in the path.  You
can use os.path.isfile() to check that a given name doesn't
refer to a subdirectory.

Once you've opened one of the files for reading, just use .read(128)
to get a string containing the first 128 bytes from the file.

-Peter



More information about the Python-list mailing list