0

Community

I'm currently trying to implement a program that includes the following shell command:

/home/myfolder/ParZu/parzu -l tagged < /home/myfolder/raw_text_de/raw_text_de0.txt >/home/myfolder/raw_text_de0_parsed.txt

/home/myfolder/ParZu/parzu is the program I want to run, -l and tagged are options to go with the program and I want to feed in my raw_text_de0.txt and write the result into another file raw_text_de0_parsed.txt.

The thing is, that I have to do this more than 400 times (for file raw_text_de1.txt, raw_text_de2.txt....), which is why I really want to automatize it.

I tried several things that have been suggested here on Stackoverflow, but none seems to work. My current attempt looks as follows:

path_texts = '/home/myfolder/raw_text_de'
filename = os.listdir(path_texts)

##create list of filenames ('/home/myfolder/raw_text_de/raw_text_de0.txt', ...)
infile_list = []
for fname in filename: 
    new_filename = '/home/myfolder/raw_text_de/' + fname
    infile_list.append(new_filename)

##go through the files in the infile_list and include it in shell command
for item in infile_list: 
    p = subprocess.call(['/home/myfolder/ParZu/parzu', '-l', 'tagged', '<', item, '>', item + 'parsed.txt'])

However, this doesn't work. It apparently calls the program, but then gets stuck. I know that it is difficult to answer this question without being able to try it, but I hope someone has an idea about what could be wrong or missing.

1
  • wouldn't os.commandline() work? Commented Mar 18, 2014 at 10:08

2 Answers 2

1

subprocess.call has two arguments: stdin and stdout. Take advantage of them instead of putting the redirection operators > and < in the arguments.

In your case:

for item in infile_list: 
    infile = file(item)
    outfile = file(item + 'parsed.txt','w')
    p = subprocess.call(['/home/myfolder/ParZu/parzu', '-l', 'tagged'], stdin=infile, stdout=outfile)
    infile.close()
    outfile.close()
Sign up to request clarification or add additional context in comments.

Comments

1

It would have been better if you could print some of your outputs of that call here.

However, I guess the problem is in your parameters of that call.

Here is the proper argument list of subprocess.call()

subprocess.call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
subprocess.call(["ls", "-l"])

And you can check the return result of your call individually just to make sure it's working fine.

subprocess.check_call(args, *, stdin=None, stdout=None, stderr=None, shell=False)
subprocess.check_call(["ls", "-l"])

subprocess.check_output(args, *, stdin=None, stderr=None, shell=False, universal_newlines=False)
subprocess.check_output(["echo", "Hello World!"])

Details could be found here:

subprocess doc

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.