reading and removing first x bytes of a file
bart_nessux
bart_nessux at hotmail.com
Thu Apr 29 11:13:09 EDT 2004
Peter Hansen wrote:
> 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
Thanks Peter, the below code recursively read the first 128B... am I
right in saying that? If so, now that I can read these bytes from all
.bin files in a directory, what would be the safest and fastest way of
removing them?
Bart
import os
def pc_macbinary_fix(path):
for root, dirs, files in os.walk(path):
for fname in files:
bin = os.path.splitext(fname)
if bin[1] == '.bin':
macbinary = file(os.path.join(root,fname), 'rb').read(128)
print "The file named:", fname, "contains: ", macbinary, "\n."
path = raw_input("Absolute path to the directory that contains the bin
files: ")
pc_macbinary_fix(path)
More information about the Python-list
mailing list