[Python-bugs-list] [ python-Bugs-774546 ] popen does not like filenames with spaces

SourceForge.net noreply at sourceforge.net
Sat Nov 1 11:44:09 EST 2003


Bugs item #774546, was opened at 2003-07-20 14:29
Message generated for change (Comment added) made by astrand
You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=774546&group_id=5470

Category: Python Library
Group: Python 2.2.2
Status: Open
Resolution: None
Priority: 5
Submitted By: Philippe Fremy (pfremy)
Assigned to: Nobody/Anonymous (nobody)
Summary: popen does not like filenames with spaces

Initial Comment:
The argument for the target executable are passed as a 
string to popen. The problem is that with filenames which 
contains spaces, the argument breaking routine will fail 
and create two arguments for one filename. 
 
It would be more convenient if one could pass the 
argument to popen as a list of string. 
 
 

----------------------------------------------------------------------

Comment By: Peter Åstrand (astrand)
Date: 2003-11-01 17:44

Message:
Logged In: YES 
user_id=344921

popen5 (http://www.lysator.liu.se/~astrand/popen5/) uses a
sequence argument instead of a string, and has no problems
with whitespace. Also, it supports connecting several
subprocesses together (feeding the output from one command
into another). 

----------------------------------------------------------------------

Comment By: Philippe Fremy (pfremy)
Date: 2003-09-01 12:41

Message:
Logged In: YES 
user_id=233844

I was trying to use python as a shell replacement for simple 
scripts, but I was disapppointed by the poor level of support of 
python for those things. 
 
In my case, the list of files was pulled from a command and 
passed to another one. 
 
I have some suggestions on how to improve python shell 
scripting capability. Right now, as soon as you want to do 
something a little bit complicated, like feeding a program (let's 
say grep) from the output of another program and checking the 
output and the exit status is quite complicated. 
 
For such an interface to be good, it has to provide _very_ easy 
way to: 
- read stdout from a command 
- provide stdin to a command 
- read exit status of a command. 
- pipe a command into another one 
 
Let's say I want to run the equivalent of 
find . -name '*.cpp' | grep -i toto 
 
My suggested interface to run a simple command would be as 
following: 
cmd( "find", ".", "-name", "*.cpp")  
 
This defines a command to be run. To be versatile, this would 
have to accept although the following format: 
 
cmd( [ "find", ".", "-name", "*.cpp" ] ) 
cmd( "find", [  ".", "-name", "*.cpp" ] ) 
which are slightly different ways of spawning a command. I think 
all these three ways have a usage pattern. 
 
To actually execute the command, you woud do: 
cmd( "find", ".", "-name", "*.cpp" ).go() 
 
This create an object which has  
 
 
 
To run it, you just apply the go() method: 
 
cmd( "find", ".", "*.cpp" ).go() 
 
This could return a tuple (stdout, stderr, return_code) 
 
Now, if you want to pipe a command into another one, we can 
either override the operator | or write it manually. My initial 
command would become: 
cmd( "find", ".", "-name", "*.cpp" ).pipe().cmd( "grep", "-i", "toto" 
) 
 
I dislike using a syntax like cmd().pipe( cmd ) because piping 
multiple commands would required parenthesis nesting which 
does not look nice. The goal is to reach the simplicity of shell 
scripting. 
 
To execute the previous command, one would simple again use 
the go() method: 
 
(stdout, stderr, ret_status) = cmd( "find", ".", "-name", "*.cpp" 
).pipe().cmd( "grep", "-i", "toto" ).go() 
 
Here for my suggestion. It might be more appropriate to post it 
to a mailing list, to get more feedback ? 
 
I am sorry, I am very busy now and won't have time to 
implement this myself. 
 
 
 
 

----------------------------------------------------------------------

Comment By: Andrew Gaul (gaul)
Date: 2003-08-19 14:17

Message:
Logged In: YES 
user_id=139865

Yes, it works.  This is also the same behaviour as
os.system.  os.popen2 allows one to pass either a quoted
string or a sequence of strings (pfremy's suggested
behaviour).  It would be nice to have consistent
functionality, and using a sequence would be easier when
providing a list of files to be operated on.  I am willing
to provide a patch if this is the desired functionality.

----------------------------------------------------------------------

Comment By: Raymond Hettinger (rhettinger)
Date: 2003-07-21 01:03

Message:
Logged In: YES 
user_id=80475

Does it work for you if the filename is quoted:  "thrill 
seeker.txt" ?

----------------------------------------------------------------------

You can respond by visiting: 
https://sourceforge.net/tracker/?func=detail&atid=105470&aid=774546&group_id=5470



More information about the Python-bugs-list mailing list