1

I need to run a python script for multiple input files and for each one, I want to generate a new corresponding output file (e.g. for input_16jun.txt I want the output file to be 16jun_output.txt). I tried doing something like:

nohup python script.py input_{16..22}jun.txt > {16..22}jun_output.txt & 

But I keep getting "ambiguous redirect" error. Does anyone know how to fix this? Or any other better approach?

8
  • Try this - nohup python script.py input_{16..22}jun.txt | tee {16..22}jun_output.txt & Commented Jul 24, 2016 at 2:16
  • Still the same error :/ Commented Jul 24, 2016 at 2:19
  • just updated. Try it if it works Commented Jul 24, 2016 at 2:20
  • Looks like that ran the script for only one input file and stored the same output in every output file Commented Jul 24, 2016 at 2:24
  • i thought your input and output file name is changing in each iteration ? tee would overwrite content in file. Commented Jul 24, 2016 at 2:26

1 Answer 1

2

Looping over each input file like this with bash should work.

for f in input_*.txt; do python script.py $f > "${f:6:-4}"_output.txt; done 

Alternatively if you want to do the loop in a python script.

import glob import os input_files = glob.glob("input_*.txt") for f in input_files: os.system("python script.py {} > {}_output.txt".format(f,f.split("input_")[1].rstrip(".txt"))) 

If you want to run script.py in parallel (rather than sequentially) you can also consider using the python multiprocessing package.

Sign up to request clarification or add additional context in comments.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.