combined files together

G

Gary Wessle

Hi

is there a module to do things like concatenate all files in a given
directory into a big file, where all the files have the same data
formate?
name address phone_no.

or do I have to open each, read from old/write-or-append to new ...

thanks
 
G

Gary Herron

Gary said:
Hi

is there a module to do things like concatenate all files in a given
directory into a big file, where all the files have the same data
formate?
name address phone_no.

or do I have to open each, read from old/write-or-append to new ...

thanks
There's hardly enough code here to make a module out of this:

combined = open('...', 'wb')
for name in os.listdir(path):
infile = open(os.path.join(path,name), 'rb')
for line in infile:
combined.write(line)

It could be more efficient by reading larger chunks than single lines,
and could be more accurate by closing both input and output files when
done, but you get the point I hope.

On the other hand, if you've got the right OS, you might try something like:
os.system("cat * > combined")

Gary Herron
 
G

Gary Wessle

Gary Herron said:
There's hardly enough code here to make a module out of this:

combined = open('...', 'wb')
for name in os.listdir(path):

I need to traverse those files in the order they were created
chronologically. listdir() does not do it, is there a way besides
build a list then list.sort(), then for element in list_of_files open
element?

thanks
 
E

Eric Deveaud

Gary said:
I need to traverse those files in the order they were created
chronologically. listdir() does not do it, is there a way besides
build a list then list.sort(), then for element in list_of_files open
element?

are the name of the files describing the cration date, or have to rely on the
creation date ?

if the name allows to discriminate the chronology, check glob module.

Eric
 
G

Gary Wessle

Eric Deveaud said:
are the name of the files describing the cration date,

yes
or have to rely on the creation date ?
no



if the name allows to discriminate the chronology, check glob module.

I just tried glob, it does not put out a list with file names sorted.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,756
Messages
2,569,535
Members
45,008
Latest member
obedient dusk

Latest Threads

Top