Need help with a looping problem

Please support our Python advertiser: Programming Forums - DaniWeb Sister Site
Reply

Join Date: Aug 2009
Posts: 6
Reputation: miac09 is an unknown quantity at this point 
Solved Threads: 0
miac09 miac09 is offline Offline
Newbie Poster

Need help with a looping problem

 
0
  #1
Aug 10th, 2009
Hi,
I'm trying to run a batch of commands from a .txt file using python. ("cmd.batch.txt" = ~1000 command lines :
perl cmd.1.pl -in -parameters)

and redirect renamed output files to new folders. It works fine for the first line, but I'm having trouble getting something to loop through all of the commands in "cmd.batch.txt".

Any help would be much appreciated !
-M

import os

f=open("cmd.batch.txt")
while True:
x=f.readlines()
y=x[0][10:30]
w=y.split(" ")
os.system(x[0])
for fn in os.listdir("."):
if fn[-5:] == "ML.dS":
os.rename(fn, "./dS/"+ w[1] +"ML.dS")
if fn[-5:] == "NG.dS":
os.rename(fn, "./dS/"+ w[1] + "NG.dS")
if not x: break
x.next()
Reply With Quote Quick reply to this message  
Join Date: Dec 2006
Posts: 1,054
Reputation: woooee is a jewel in the rough woooee is a jewel in the rough woooee is a jewel in the rough 
Solved Threads: 297
woooee woooee is offline Offline
Veteran Poster

Re: Need help with a looping problem

 
0
  #2
Aug 10th, 2009
There are two problems

while True:
x=f.readlines()
You read the entire file on every loop, so you willl always get the first rec of the newly read list.

if not x: break
x.next()
This may exit if you have any blank lines in the file.

Instead, you want to loop through all of the records with a for() loop
  1. input_list=open("cmd.batch.txt").readlines()
  2. for rec in input_list:
  3. print rec
  4. if len( rec.strip() ) < 1:
  5. print " that rec was an empty line"
Linux counter #99383
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 6
Reputation: miac09 is an unknown quantity at this point 
Solved Threads: 0
miac09 miac09 is offline Offline
Newbie Poster

Re: Need help with a looping problem

 
0
  #3
Aug 11th, 2009
Thanks so much for your help! It worked!!
Now it is going line by line through the command list -

But, I have another problem which is the program outputs files with the same basename so they are overwritten everytime the program runs. I have written the following code to change the basenames of the output files when they are written to the directory. But, it only works for the first couple of files - what's the best way to modify this portion of the code to keep it looping ?
Thanks again-

for rec in input_list:
rec.strip()
os.system(rec)
count=1
for fn in os.listdir("."):
if fn[-5:] == "ML.dS":
os.rename(fn, "%01iML.dS" %count)
count=count+1
Reply With Quote Quick reply to this message  
Join Date: Aug 2009
Posts: 56
Reputation: willygstyle is an unknown quantity at this point 
Solved Threads: 6
willygstyle willygstyle is offline Offline
Junior Poster in Training

Re: Need help with a looping problem

 
0
  #4
Aug 11th, 2009
If you want something to keep looping couldn't you just wrap it up in a while loop?
While count < somenumber:
do these things
Reply With Quote Quick reply to this message  
Reply

This thread is more than three months old.
Perhaps start a new thread instead?
Message:



Similar Threads
Other Threads in the Python Forum
Thread Tools Search this Thread



Tag cloud for Python
About Us | Contact Us | Advertise | DaniWeb | Acceptable Use Policy | RSS Feed

©2003 - 2009 DaniWeb® LLC