reading and removing first x bytes of a file

Discussion in 'Python' started by bart_nessux, Apr 29, 2004.

  1. bart_nessux

    bart_nessux Guest

    Hello,

    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?

    Thanks,
    Bart
     
    bart_nessux, Apr 29, 2004
    #1
    1. Advertising

  2. bart_nessux

    Peter Hansen Guest

    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
     
    Peter Hansen, Apr 29, 2004
    #2
    1. Advertising

  3. bart_nessux

    Brian Gough Guest

    bart_nessux <> writes:

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


    You can use the file object .seek() and .read() methods to move around
    in the file and read parts of it.

    There is an example in the "Input and Output" chapter of the Python
    Tutorial.

    --
    Brian Gough

    Network Theory Ltd,
    Publishing the Python Manuals --- http://www.network-theory.co.uk/
     
    Brian Gough, Apr 29, 2004
    #3
  4. bart_nessux

    bart_nessux Guest

    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)
     
    bart_nessux, Apr 29, 2004
    #4
  5. bart_nessux

    Peter Hansen Guest

    bart_nessux wrote:

    > Thanks Peter, the below code recursively read the first 128B... am I
    > right in saying that?


    Well, your indentation is screwed up, for one thing, so I can't
    guarantee the code does what you want. I'll leave actually testing
    it up to you...

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


    Define 'safe' and describe how fast you want it to run. <wink>

    Anyway, you can't actually "remove" bytes from the files, so
    what you really need to do is then read the *rest* of the bytes
    (i.e. keep the file open after the first read, and do a .read()
    of the rest of the data) and then write the shortened data to
    a temporary file (module tempfile can be helpful here), then
    once that's worked, use os.remove to remove the old file, and
    os.rename to rename the temp file to the same name as the old
    file.

    -Peter
     
    Peter Hansen, Apr 29, 2004
    #5
  6. bart_nessux

    Terry Reedy Guest

    "Peter Hansen" <> wrote in message
    news:...
    > Anyway, you can't actually "remove" bytes from the files, so
    > what you really need to do is then read the *rest* of the bytes
    > (i.e. keep the file open after the first read, and do a .read()
    > of the rest of the data) and then write the shortened data to
    > a temporary file (module tempfile can be helpful here), then
    > once that's worked, use os.remove to remove the old file, and
    > os.rename to rename the temp file to the same name as the old
    > file.


    IF the remainder of the file fits in memory, then it can 'moved up' by
    rewriting same file after seeking to beginning of file. Don't know how
    safe on MAC OSes (ie, degree to which write is atomic). Of course, if data
    are important, one should have at least a few minutes of battery backup.

    TJR
     
    Terry Reedy, Apr 29, 2004
    #6
  7. bart_nessux

    Peter Hansen Guest

    Terry Reedy wrote:

    > IF the remainder of the file fits in memory, then it can 'moved up' by
    > rewriting same file after seeking to beginning of file. Don't know how
    > safe on MAC OSes (ie, degree to which write is atomic). Of course, if data
    > are important, one should have at least a few minutes of battery backup.


    I think it's a fair assumption that when the OP asked for a "safe"
    way, he meant at least safer than that. If power failed in the
    middle of the update, or a system crash occurred, the file could
    well be unrecoverable...

    -Peter
     
    Peter Hansen, Apr 29, 2004
    #7
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. Jason Collins
    Replies:
    3
    Views:
    6,022
    Jason Collins
    Feb 18, 2004
  2. Yandos
    Replies:
    12
    Views:
    5,127
    Pete Becker
    Sep 15, 2005
  3. BogusException
    Replies:
    7
    Views:
    335
  4. George2
    Replies:
    1
    Views:
    884
    Victor Bazarov
    Jan 9, 2008
  5. Guest
    Replies:
    6
    Views:
    1,695
    Guest
    Apr 25, 2010
Loading...

Share This Page