473,761 Members | 7,351 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

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_t ext(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.Pope n("%s '%s'" % (antiword, word_doc), shell=True,
stdout=subproce ss.PIPE)
doc_text = p.stdout.read()
return doc_text

thx,

rd

Nov 6 '07 #1
3 6024
* BartlebyScriven er (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_t ext(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, BartlebyScriven er
<bs**********@g mail.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.a rwrote:
>
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.Pope n([antiword, word_doc], stdout=subproce ss.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
6297
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", stdin=subprocess.PIPE) # Run an octave called "startup". oct.communicate("startup") # Change directory inside octave.
3
3598
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 <- python script core/ <- directory main <- fortran executable in the core directory
5
14328
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 consistently gets an "invalid operand" IOError when write() is called on the pipe. So I switched to subprocess. It works fine when executed "normally" (e.g. "python progname.py"), but when bundled by py2exe, it always does this:
12
10991
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 the old module and cmd is the command I wish to try) all the output seems to be returned as one line (at least when I run the program in spe). import subprocess from os import system cmd = """gawk -f altertime.awk -v time_offset=4 -v
6
2412
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 it to do but when I am asking it to load a file it doesn't do anything. Is there something I need to do to push the data through the pipe?? Here is a couple different ways I am trying to do it. def load_instrument(instr_name, csdname): "load...
0
805
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
1223
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 problem is on Linux as well)? TJG
3
2396
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 created in the console. The fortran program can take up to 20 minuttes to finish and at the moment the I will first see any output after the fortran program is done. How make my function write the output of the process as it
3
8730
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 should inherit file descriptors from the parent if close_fds=False on the suprocess.Popen command line. This code doesn't work under Window, but gives "bad file descriptor" when trying to read from the pipe in the child process. I have some example...
0
9522
marktang
by: marktang | last post by:
ONU (Optical Network Unit) is one of the key components for providing high-speed Internet services. Its primary function is to act as an endpoint device located at the user's premises. However, people are often confused as to whether an ONU can Work As a Router. In this blog post, we’ll explore What is ONU, What Is Router, ONU & Router’s main usage, and What is the difference between ONU and Router. Let’s take a closer look ! Part I. Meaning of...
0
9336
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 effortlessly switch the default language on Windows 10 without reinstalling. I'll walk you through it. First, let's disable language synchronization. With a Microsoft account, language settings sync across devices. To prevent any complications,...
0
9948
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 tapestry of website design and digital marketing. It's not merely about having a website; it's about crafting an immersive digital experience that captivates audiences and drives business growth. The Art of Business Website Design Your website is...
1
9902
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 Update option using the Control Panel or Settings app; it automatically checks for updates and installs any it finds, whether you like it or not. For most users, this new feature is actually very convenient. If you want to control the update process,...
0
9765
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 protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
8770
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, and deployment—without human intervention. Imagine an AI that can take a project description, break it down, write the code, debug it, and then launch it, all on its own.... Now, this would greatly impact the work of software developers. The idea...
1
7327
isladogs
by: isladogs | last post by:
The next Access Europe User Group meeting will be on Wednesday 1 May 2024 starting at 18:00 UK time (6PM UTC+1) and finishing by 19:30 (7.30PM). In this session, we are pleased to welcome a new presenter, Adolph Dupré who will be discussing some powerful techniques for using class modules. He will explain when you may want to use classes instead of User Defined Types (UDT). For example, to manage the data in unbound forms. Adolph will...
0
5215
by: TSSRALBI | last post by:
Hello I'm a network technician in training and I need your help. I am currently learning how to create and manage the different types of VPNs and I have a question about LAN-to-LAN VPNs. The last exercise I practiced was to create a LAN-to-LAN VPN between two Pfsense firewalls, by using IPSEC protocols. I succeeded, with both firewalls in the same network. But I'm wondering if it's possible to do the same thing, with 2 Pfsense firewalls...
0
5364
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?

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.