Go Back   Rhinocerus > Newsgroup > Newsgroup comp.lang.python

Reply
 
Thread Tools Display Modes
  #1 (permalink)  
Old 07-13-2012, 07:34 AM
nuffi
Guest
 
Posts: n/a
Default help needed with subprocess, pipes and parameters


If I copy and paste the following command into a command window, it does what I need.

c:\Programs\bob\bob.exe -x -y "C:\text\path\to some\file.txt" | c:\Programs\kate\kate.exe -A 2 --dc "Print Media Is Dead" --da "Author" --dt "Title" --hf "Times" --bb "14" --aa "" --font "Ariel" - "C:\rtf\path\to some\file.rtf"

My mission is to recreate this command within a python script, so that I can pass a bunch of different parameters into it, and use it as a batch over a bunch of different papers.

http://docs.python.org/library/subprocess.html seems to be the thing to usein python 2.7.3. I also checked out http://www.doughellmann.com/PyMOTW/subprocess/.

My attempts run fine, create destination folders ok and prints done but don't actually seem to process the file. Is there some way to get subprocessto output the command it's generating so I can see what I'm doing wrong, rather than just the output of the command?

How can I chekc that kate's opening the pipe left from bob? Bob may takesome time to execute, will that matter?


The code I came up with looks like this:

import os, glob, subprocess

sourceDir = "c:\\text\\"
destDir = "c:\\rtf\\"
bobPath = "C:\\Programs\\bob\\bob.exe"
katePath = "C:\\Programs\\kate\\kate.exe"

def get_new_path(doc):
blah = doc.replace(sourceDir, destDir)
if not os.path.isdir(blah):
os.makedirs(blah)
rtf = blah.replace('.txt', '.rtf')
pathString = '- "' + (os.path.join(rtf)) + '"'
return(pathString)


def convert_doc(doc):
dc = '--dc "Print Media Is Dead"'
da = '--da "Author"'
dt = '--dt "Title"'
hf = '--hf "Times"'
fn = '--font "Ariel"'
bb = '--bb "14"'
docpath = '"' + (os.path.join(doc)) + '"'
path = get_new_path(doc)
A = '-A 2'
bob = subprocess.Popen([bobPath, '-x', '-y', docpath], stdout = subprocess.PIPE,)
kate = subprocess.Popen([katePath, A, dc, da, dt, hf, fn, bb, path], stdin = bob.stdout, stdout = subprocess.PIPE,)
end_of_pipe = kate.stdout
#print kate
#subprocess.call(['echo', "Test peice of text", '>', 'D:\\output.txt'])
for line in end_of_pipe:
print '\t', blah.strip()
#raw_input()
return('done')


test = convert_doc("c:\\text\\path to\\some\\specific text file.txt")
print(blah)


==

Thanks for looking :-)
Reply With Quote
Alt Today
Advertising
 
and become member of Rhinocerus
Standard Sponsored Links

  #2 (permalink)  
Old 07-17-2012, 10:56 PM
John Pote
Guest
 
Posts: n/a
Default Re: help needed with subprocess, pipes and parameters

nuffi,

Have you tried running your piped commands

c:\Programs\bob\bob.exe -x -y "C:\text\path\to some\file.txt" |
c:\Programs\kate\kate.exe -A 2 --dc "Print Media Is Dead" --da "Author"
--dt "Title" --hf "Times" --bb "14" --aa "" --font "Ariel" -
"C:\rtf\path\to some\file.rtf"

in a single instance of subprocess? Start it up and use the poll method
to wait until the subprocess has terminated. Then open the output file
to get the results.

My recent (and single, hence limited, experience) of using subprocess
indicated that the spawned process may take a little time to start up. Try

bob = subprocess.Popen([bobPath, '-x', '-y', docpath], stdout =
subprocess.PIPE,)
time.sleep( 0.5 ) #experiment with the delay
kate = subprocess.Popen([katePath, A, dc, da, dt, hf, fn, bb, path],
stdin = bob.stdout, stdout = subprocess.PIPE,)

Never looked under the subprocess hood but maybe there's a race
condition with the kate subprocess starting before the bob subprocess
has set up its stdout. It's unlikely but so easy to check with a sleep
between the subprocesses it has to be worth a go.

John


On 13/07/2012 08:34, nuffi wrote:
>
> If I copy and paste the following command into a command window, it does what I need.
>
> c:\Programs\bob\bob.exe -x -y "C:\text\path\to some\file.txt" | c:\Programs\kate\kate.exe -A 2 --dc "Print Media Is Dead" --da "Author" --dt "Title" --hf "Times" --bb "14" --aa "" --font "Ariel" - "C:\rtf\path\to some\file.rtf"
>
> My mission is to recreate this command within a python script, so that I can pass a bunch of different parameters into it, and use it as a batch over a bunch of different papers.
>
> http://docs.python.org/library/subprocess.html seems to be the thing to use in python 2.7.3. I also checked out http://www.doughellmann.com/PyMOTW/subprocess/.
>
> My attempts run fine, create destination folders ok and prints done but don't actually seem to process the file. Is there some way to get subprocess to output the command it's generating so I can see what I'm doing wrong, rather than just the output of the command?
>
> How can I chekc that kate's opening the pipe left from bob? Bob may take some time to execute, will that matter?
>
>
> The code I came up with looks like this:
>
> import os, glob, subprocess
>
> sourceDir = "c:\\text\\"
> destDir = "c:\\rtf\\"
> bobPath = "C:\\Programs\\bob\\bob.exe"
> katePath = "C:\\Programs\\kate\\kate.exe"
>
> def get_new_path(doc):
> blah = doc.replace(sourceDir, destDir)
> if not os.path.isdir(blah):
> os.makedirs(blah)
> rtf = blah.replace('.txt', '.rtf')
> pathString = '- "' + (os.path.join(rtf)) + '"'
> return(pathString)
>
>
> def convert_doc(doc):
> dc = '--dc "Print Media Is Dead"'
> da = '--da "Author"'
> dt = '--dt "Title"'
> hf = '--hf "Times"'
> fn = '--font "Ariel"'
> bb = '--bb "14"'
> docpath = '"' + (os.path.join(doc)) + '"'
> path = get_new_path(doc)
> A = '-A 2'
> bob = subprocess.Popen([bobPath, '-x', '-y', docpath], stdout = subprocess.PIPE,)
> kate = subprocess.Popen([katePath, A, dc, da, dt, hf, fn, bb, path], stdin = bob.stdout, stdout = subprocess.PIPE,)
> end_of_pipe = kate.stdout
> #print kate
> #subprocess.call(['echo', "Test peice of text", '>', 'D:\\output.txt'])
> for line in end_of_pipe:
> print '\t', blah.strip()
> #raw_input()
> return('done')
>
>
> test = convert_doc("c:\\text\\path to\\some\\specific text file.txt")
> print(blah)
>
>
> ==
>
> Thanks for looking :-)
>




--- Posted via news://freenews.netfront.net/ - Complaints to news@netfront.net ---
Reply With Quote
 
Reply

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off




All times are GMT. The time now is 03:56 AM.


Copyright ©2009

LinkBacks Enabled by vBSEO 3.3.0 RC2 © 2009, Crawlability, Inc.