473,383 Members | 1,861 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

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

Process multiple files

Say I have multiple text files in a single directory, for illustration
they are called "spam.txt" and "eggs.txt". All of these text files are
organized in exactly the same way. I have written a program that parses
each file one at a time. In other words, I need to run my program each
time I want to process one of these files.

However, because I have hundreds of these files I would like to be able
to process them all in one fell swoop. The current program is something
like this:

sample.py
new_file = open('filename.txt', 'w')
params = open('eggs.txt', 'r')
do all the python stuff here
new_file.close()

If these files followed a naming convention such as 1.txt and 2.txt I
can easily see how these could be parsed consecutively in a loop.
However, they are not and so is it possible to modify this code such
that I can tell python to parse all .txt files in a certain directory
and then to save them as separate files? For instance, using the example
above, python would parse both spam.txt and eggs.txt and then save 2
different files, say as spam_parsed.txt and eggs_parsed.txt.

Thanks


Jun 27 '08 #1
1 2052
"Doran, Harold" <HD****@air.orgwrote:
If these files followed a naming convention such as 1.txt and 2.txt I
can easily see how these could be parsed consecutively in a loop.
However, they are not and so is it possible to modify this code such
that I can tell python to parse all .txt files in a certain directory
and then to save them as separate files? For instance, using the example
above, python would parse both spam.txt and eggs.txt and then save 2
different files, say as spam_parsed.txt and eggs_parsed.txt.
This isn't exactly what you asked for, but have you looked at the fileinput
module: it will process a list of filenames pointing stdin at each file in
turn, optionally it can work inplace, but without that option I think it is
pretty close to what you want (warning, untested code follows):

output = None
for line in fileinput.input(n for n in glob.glob('*.txt')
if not n.endswith('_parsed.txt')):

if fileinput.isfirstline():
if output: output.close()
output = open(fileinput.filename().replace('.', '_parsed.', 1),
'w')

print >>output, "parsed:", line.strip()

if output:
output.close()
Jun 27 '08 #2

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

Similar topics

6
by: m | last post by:
Hello, I have an application that processes thousands of files each day. The filenames and various related file information is retrieved, related filenames are associate and placed in a linked...
2
by: Jeffrey Palermo, MCAD.Net | last post by:
I have some classes that control file processing in c#. The files come from a mainframe and may take some time for each to process. I use the Threadpool to process multiple files at once. Each...
6
by: gizmo | last post by:
I have a requirement to initiate more than one instance of an application using the filenames. (the example below will start two instances of MS Word). My problem is that I need to kill each...
3
by: Richard Lewis Haggard | last post by:
We are having a lot of trouble with problems relating to failures relating to 'The located assembly's manifest definition with name 'xxx' does not match the assembly reference" but none of us here...
0
by: vasikaran | last post by:
Hi , i hava one ftp script in batch file programming , ftp script is working fine.. but my is i dont know how much files and what are files avaiulable in remote folder ,,,, if any errors or...
0
by: Paul Anton Letnes | last post by:
Funny, I'm just doing exactly this: import os def main(): dataFolder = 'data/' fileList = os.listdir(dataFolder) for file in fileList:
4
by: tdahsu | last post by:
All, I'd appreciate any help. I've got a list of files in a directory, and I'd like to iterate through that list and process each one. Rather than do that serially, I was thinking I should...
7
by: dieter | last post by:
Hi, Overview ======= I'm doing some simple file manipulation work and the process gets "Killed" everytime I run it. No traceback, no segfault... just the word "Killed" in the bash shell and...
0
by: lancer6238 | last post by:
Hi all, I'm writing a program using libpcap, and I have multiple pcap files in a folder that I want to capture. I currently have handle = pcap_open_offline("/data/traffic/pcap1.pcap",...
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...
0
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 3 Apr 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome former...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: Charles Arthur | last post by:
How do i turn on java script on a villaon, callus and itel keypad mobile phone
0
by: aa123db | last post by:
Variable and constants Use var or let for variables and const fror constants. Var foo ='bar'; Let foo ='bar';const baz ='bar'; Functions function $name$ ($parameters$) { } ...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
1
by: nemocccc | last post by:
hello, everyone, I want to develop a software for my android phone for daily needs, any suggestions?
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...

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.