Reputation: 1257
I have a python script (script.py) that requires one file per input (input.txt) and one file per output (output.txt). Plus, the script requires additional data from model.pcl. I execute script through the console. For windows it looks like this:
type input.txt | python script.py model.pcl output.txt
I would like to apply this script to 10 thousand files and save the result in separate txt files.
I know how to do it with python:
import subprocess
for i in range(1, 10001):
e = 'type input_%s.txt | python script.py model.pcl output/output_%s.txt' % (i,i)
subprocess.call(e, shell=True)
But it is not an ideal solution because in this approach I cannot specify a folder for an input file (input/input_%s.txt). If I try to do it I get an error:
the syntax of the command is incorrect.
Are there other options for a Windows machine?
Upvotes: 2
Views: 568
Reputation: 3264
This should do the needful from the Windows CMD Prompt:
FOR /L %# IN (1,1,10001) DO @TYPE "C:\Path\To\input_%#.txt" | python script.py model.pcl "C:\Path\To\output_%#.txt"
Upvotes: 0
Reputation: 1112
I've faced a similar issue like this in python. It worked for me when I added all the items in a list.
import subprocess
for i in range(1, 10001):
e = ['type input_%s.txt | python script.py model.pcl output/output_%s.txt' % (i,i)]
subprocess.call(e, shell=True)
Can you test it and let me know if it works for you?
Upvotes: 0
Reputation: 79
One option is something similar to this
#Path to all files
path = os.getcwd()
#Puts file names to a variable
files = os.listdir(path)
#Gets just the .txt
txtFiles = glob.glob(path + "/*.txt")
This would allow you to do all .txt files in a specific folder ideally the one that the script is placed in but that can be asily modified
Upvotes: -1