472,789 Members | 952 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 472,789 software developers and data experts.

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

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!

Jul 18 '05 #1
4 3001
go*************@hotmail.com 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
Jul 18 '05 #2
Would like to use text files, its really for my own learning,

but I suppose a Zip would work

cheers

Jul 18 '05 #3
go*************@hotmail.com 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 <mw*@mired.org> http://www.mired.org/home/mwm/
Independent WWW/Perforce/FreeBSD/Unix consultant, email for more information.
Jul 18 '05 #4
go*************@hotmail.com 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

Jul 18 '05 #5

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

7
by: jsale | last post by:
I have made an ASP.NET web application that connects to SQL Server, reading and writing data using classes. I was recommended to use session objects to store the data per user, because each user...
12
by: Adam J. Schaff | last post by:
I am writing a quick program to edit a binary file that contains file paths (amongst other things). If I look at the files in notepad, they look like: ...
6
by: Kevin Chambers | last post by:
Hi all-- In an attempt to commit an Access MDB to a versioning system (subversion), I'm trying to figure out how to convert a jet table's metadata to text, a la SaveAsText. The end goal is to...
10
by: joelagnel | last post by:
hi friends, i've been having this confusion for about a year, i want to know the exact difference between text and binary files. using the fwrite function in c, i wrote 2 bytes of integers in...
0
by: Masa Ito | last post by:
I have pipe delimited (and comma/tab) files that I read with JET using a schema file. Occasionally a field has multiple quotes (") inside a single field - which chokes the line (the rest of the...
6
by: arne.muller | last post by:
Hello, I've come across some problems reading strucutres from binary files. Basically I've some strutures typedef struct { int i; double x; int n; double *mz;
9
by: andy.z | last post by:
If 2 people try to access the same text file at the same time to write to it - what happens in PHP ? What I mean is - presumably the first will be ok - But what will the second person actually...
1
by: Xah Lee | last post by:
Text Processing with Emacs Lisp Xah Lee, 2007-10-29 This page gives a outline of how to use emacs lisp to do text processing, using a specific real-world problem as example. If you don't know...
6
by: John Salerno | last post by:
Before I try this and destroy my computer :) I just wanted to see if this would even work at all. Is it possible to read a binary file such as an mp3 or an avi, put its contents into a new file,...
0
by: Rina0 | last post by:
Cybersecurity engineering is a specialized field that focuses on the design, development, and implementation of systems, processes, and technologies that protect against cyber threats and...
0
by: erikbower65 | last post by:
Using CodiumAI's pr-agent is simple and powerful. Follow these steps: 1. Install CodiumAI CLI: Ensure Node.js is installed, then run 'npm install -g codiumai' in the terminal. 2. Connect to...
0
by: erikbower65 | last post by:
Here's a concise step-by-step guide for manually installing IntelliJ IDEA: 1. Download: Visit the official JetBrains website and download the IntelliJ IDEA Community or Ultimate edition based on...
0
by: kcodez | last post by:
As a H5 game development enthusiast, I recently wrote a very interesting little game - Toy Claw ((http://claw.kjeek.com/))。Here I will summarize and share the development experience here, and hope it...
0
by: Taofi | last post by:
I try to insert a new record but the error message says the number of query names and destination fields are not the same This are my field names ID, Budgeted, Actual, Status and Differences ...
5
by: DJRhino | last post by:
Private Sub CboDrawingID_BeforeUpdate(Cancel As Integer) If = 310029923 Or 310030138 Or 310030152 Or 310030346 Or 310030348 Or _ 310030356 Or 310030359 Or 310030362 Or...
0
by: lllomh | last post by:
Define the method first this.state = { buttonBackgroundColor: 'green', isBlinking: false, // A new status is added to identify whether the button is blinking or not } autoStart=()=>{
0
by: lllomh | last post by:
How does React native implement an English player?
2
by: DJRhino | last post by:
Was curious if anyone else was having this same issue or not.... I was just Up/Down graded to windows 11 and now my access combo boxes are not acting right. With win 10 I could start typing...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.