Text files read multiple files into single file, and then recreate the multiple files

Discussion in 'Python' started by googlinggoogler@hotmail.com, Feb 12, 2005.

  1. Guest

    Hiya,

    The title says it all really, but im a newbie to python sort of. I can
    read in files and write files no probs.

    But what I want to do is read in a couple of files and output them to
    one single file, but then be able to take this one single file and
    recreate the files I put into it.

    Im really at a loss as to how I go about recovering the files?
    obviously if i scan for a string that specifys the start and end of
    each file, theres the chance that the file might contain this term to
    which would split the files into unwanted chucks of file, which wouldnt
    be wanted.

    Any ideas? code snippets?

    Im very grateful!
    , Feb 12, 2005
    #1
    1. Advertising

  2. Re: Text files read multiple files into single file, and then recreatethe multiple files

    wrote:
    > Hiya,
    >
    > The title says it all really, but im a newbie to python sort of. I can
    > read in files and write files no probs.
    >
    > But what I want to do is read in a couple of files and output them to
    > one single file, but then be able to take this one single file and
    > recreate the files I put into it.


    A ZIP archive perhaps?
    See the zipfile module.


    > Im really at a loss as to how I go about recovering the files?
    > obviously if i scan for a string that specifys the start and end of
    > each file, theres the chance that the file might contain this term to
    > which would split the files into unwanted chucks of file, which wouldnt
    > be wanted.


    ZIP archives are binary compressed files. Is this okay or do
    you require the file to be a text file?

    --Irmen
    Irmen de Jong, Feb 12, 2005
    #2
    1. Advertising

  3. Guest

    Would like to use text files, its really for my own learning,

    but I suppose a Zip would work

    cheers
    , Feb 12, 2005
    #3
  4. Mike Meyer Guest

    Re: Text files read multiple files into single file, and thenrecreate the multiple files

    writes:

    > Would like to use text files, its really for my own learning,
    >
    > but I suppose a Zip would work


    How about outputing a file as:

    LENGTH=######
    <file-contents>

    in your joined file? That's a minimalistic archive format - one ASCII
    header line that provides just enough information to extract the
    file. Be sure to open all files [rw]b, though.

    <mike
    --
    Mike Meyer <> http://www.mired.org/home/mwm/
    Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
    Mike Meyer, Feb 13, 2005
    #4
  5. M.E.Farmer Guest

    wrote:
    > Hiya,
    >
    > The title says it all really, but im a newbie to python sort of. I

    can
    > read in files and write files no probs.
    >
    > But what I want to do is read in a couple of files and output them to
    > one single file, but then be able to take this one single file and
    > recreate the files I put into it.
    >
    > Im really at a loss as to how I go about recovering the files?
    > obviously if i scan for a string that specifys the start and end of
    > each file, theres the chance that the file might contain this term to
    > which would split the files into unwanted chucks of file, which

    wouldnt
    > be wanted.
    >
    > Any ideas? code snippets?
    >
    > Im very grateful!

    Ok, for the seperating of the records you could something like this.
    This function returns a generator that can iterate thru your records.
    Py> #Single file multi-record seperator
    .... def recordbreaker(record, seperator='#NextRecord#'):
    .... seplen = len(seperator)
    .... rec = open(record ,'rb')
    .... a =[]
    .... for line in rec:
    .... sep = line.find(seperator)
    .... if sep != -1:
    .... a.append(line[:sep])
    .... out = ''.join(a)
    .... a =[]
    .... a.append(line[sep+seplen:].lstrip())
    .... yield out
    .... else:
    .... a.append(line)
    .... if a:
    .... yield ''.join(a)
    .... rec.close()
    ....
    Py> # to use it directly
    Py> records = recordbreaker('myrecords.txt')
    Py> first_record = records.next()
    Py> second_record = records.next()
    Py> # or you can do this
    Py> for record in records:
    Py> print record

    Getting them in a single file is easy, and will be an excercise left to
    the reader.
    The fileinput module is your friend it is in the standard library.
    Be sure to check it out if you haven't already.
    hth,
    M.E.Farmer
    M.E.Farmer, Feb 13, 2005
    #5
    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. Mufasa
    Replies:
    7
    Views:
    400
    Alexey Smirnov
    Sep 4, 2007
  2. Replies:
    0
    Views:
    252
  3. Martin Foster
    Replies:
    4
    Views:
    141
    Martin Foster
    Dec 8, 2003
  4. ela
    Replies:
    12
    Views:
    323
    Uri Guttman
    Apr 6, 2009
  5. Replies:
    4
    Views:
    517
    cwdjrxyz
    Jan 17, 2006
Loading...

Share This Page