473,408 Members | 1,784 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,408 software developers and data experts.

subprocess chokes on spaces in path

Using bash on Debian Etch.

If word_doc = sys.argv[1] and it's a file name like My\ Word.doc this
function reads My and Word as two separate files unless the second
'%s' is quoted. Took me a lot of trial and error to discover. Is this
the most elegant way to do it? I was using popen originally, then saw
some threads suggesting subprocess cured the spaces in path problem.

def get_MSWordDoc_text(word_doc):
"""Harvests text from an MSWord doc using antiword."""
antiword = "/usr/bin/antiword"

# Note the extra single quotes around the second '%s'
# without these quotes, bash chokes on paths with spaces in them
# says can't open My ; can't open Word
# using new subprocess module, the extra '%s' shouldn't be
necessary?
# but I could not get to work
# see Beazley 2nd Ed. page 340

p = subprocess.Popen("%s '%s'" % (antiword, word_doc), shell=True,
stdout=subprocess.PIPE)
doc_text = p.stdout.read()
return doc_text

thx,

rd

Nov 6 '07 #1
3 5967
* BartlebyScrivener (Tue, 06 Nov 2007 20:32:33 -0000)
Using bash on Debian Etch.

If word_doc = sys.argv[1] and it's a file name like My\ Word.doc this
function reads My and Word as two separate files unless the second
'%s' is quoted. Took me a lot of trial and error to discover. Is this
the most elegant way to do it? I was using popen originally, then saw
some threads suggesting subprocess cured the spaces in path problem.

def get_MSWordDoc_text(word_doc):
"""Harvests text from an MSWord doc using antiword."""
antiword = "/usr/bin/antiword"

# Note the extra single quotes around the second '%s'
# without these quotes, bash chokes on paths with spaces in them
# says can't open My ; can't open Word
If bash has problems with spaces then the obvious thing to do is not
to use bash, right?!

Thorsten
Nov 6 '07 #2
En Tue, 06 Nov 2007 17:32:33 -0300, BartlebyScrivener
<bs**********@gmail.comescribió:
Using bash on Debian Etch.

If word_doc = sys.argv[1] and it's a file name like My\ Word.doc this
function reads My and Word as two separate files unless the second
'%s' is quoted. Took me a lot of trial and error to discover. Is this
the most elegant way to do it? I was using popen originally, then saw
some threads suggesting subprocess cured the spaces in path problem.
Use a list of arguments [antiword, word_doc] and let subprocess handle the
spaces the right way.
--
Gabriel Genellina

Nov 6 '07 #3
On Nov 6, 2:48 pm, "Gabriel Genellina" <gagsl-...@yahoo.com.arwrote:
>
Use a list of arguments [antiword, word_doc] and let subprocess handle the
spaces the right way.
Got it working. Thank you both.

p = subprocess.Popen([antiword, word_doc], stdout=subprocess.PIPE)
doc_text = p.stdout.read()

rd


Nov 7 '07 #4

This thread has been closed and replies have been disabled. Please start a new discussion.

Similar topics

1
by: Edward C. Jones | last post by:
I have a program named "octave" (a Matlab clone). It runs in a terminal, types a prompt and waits for the user to type something. If I try # Run octave. oct = subprocess.Popen("octave",...
3
by: Ivan Vinogradov | last post by:
Dear All, I would greatly appreciate a nudge in the right direction concerning the use of cwd argument in the call function from subprocess module. The setup is as follows: driver.py <-...
5
by: Grant Edwards | last post by:
I'm trying to use the py-gnuplot module on windows, and have been unable to get it to work reliably under Win2K and WinXP. By default, it uses popen(gnuplotcmd,'w'), but in some situations that...
12
by: Eric_Dexter | last post by:
I am trying to modify a programming example and I am coming up with two problems... first is that I can't seem to pass along the arguments to the external command (I have been able to do that with...
6
by: Eric_Dexter | last post by:
I am having trouble contolling vim with subprocess on a windows machine. It appears that vim comes up on the machine all right and it sometimes looks like it is doing the searchs what I am asking...
0
by: Michel Lespinasse | last post by:
Hi, I hit an issue with the following python code: try: get_orient = subprocess.Popen (, stdin = subprocess.PIPE, stdout = subprocess.PIPE) orient = get_orient.communicate () except:
0
by: Tim Golden | last post by:
Michel Lespinasse wrote: This looks like a duplicate of http://bugs.python.org/issue3210. Can you confirm if this seems likely (and, if so, perhaps add a note to the bug indicating that the...
3
by: Thomas Jansson | last post by:
Dear all I have tkinkter based frontend to a Fortran based program. I use subprocess to launch the fortran program as a child process and I wish to see the output of the fortran program as it is...
3
by: Jeremy Sanders | last post by:
Hi - I have some code which works under linux. It starts a remote python process using subprocess and communicates to it via a pipe created by os.pipe. As far as I understand, child processes...
0
BarryA
by: BarryA | last post by:
What are the essential steps and strategies outlined in the Data Structures and Algorithms (DSA) roadmap for aspiring data scientists? How can individuals effectively utilize this roadmap to progress...
1
by: Sonnysonu | last post by:
This is the data of csv file 1 2 3 1 2 3 1 2 3 1 2 3 2 3 2 3 3 the lengths should be different i have to store the data by column-wise with in the specific length. suppose the i have to...
0
by: Hystou | last post by:
There are some requirements for setting up RAID: 1. The motherboard and BIOS support RAID configuration. 2. The motherboard has 2 or more available SATA protocol SSD/HDD slots (including MSATA, M.2...
0
by: Hystou | last post by:
Most computers default to English, but sometimes we require a different language, especially when relocating. Forgot to request a specific language before your computer shipped? No problem! You can...
0
jinu1996
by: jinu1996 | last post by:
In today's digital age, having a compelling online presence is paramount for businesses aiming to thrive in a competitive landscape. At the heart of this digital strategy lies an intricately woven...
0
by: Hystou | last post by:
Overview: Windows 11 and 10 have less user interface control over operating system update behaviour than previous versions of Windows. In Windows 11 and 10, there is no way to turn off the Windows...
0
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each...
0
agi2029
by: agi2029 | last post by:
Let's talk about the concept of autonomous AI software engineers and no-code agents. These AIs are designed to manage the entire lifecycle of a software development project—planning, coding, testing,...
0
by: conductexam | last post by:
I have .net C# application in which I am extracting data from word file and save it in database particularly. To store word all data as it is I am converting the whole word file firstly in HTML and...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.