help needed with subprocess, pipes and parameters

Chris Rebert clp2 at rebertia.com
Sun Jul 15 00:50:54 EDT 2012


On Friday, July 13, 2012, 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.
>
<snip>
>
> 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,)


Your tokenization of the command is wrong. Read the Note box in the
subprocess docs for guidance on how to get it right:
http://docs.python.org/library/subprocess.html#popen-constructor

Cheers,
Chris via iPhone


-- 
Cheers,
Chris
--
http://rebertia.com
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/python-list/attachments/20120714/c1526af3/attachment.html>


More information about the Python-list mailing list