473,765 Members | 1,994 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

subprocess.pope n function with quotes

Hi,
i'm trying to call subprocess.pope n on the 'rename' function in
linux. When I run the command from the shell, like so:

rename -vn 's/\.htm$/\.html/' *.htm

it works fine... however when I try to do it in python like so:
p = subprocess.Pope n(["rename","-vn","'s/\.htm$/
\.html/'","*.htm"],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)

print p.communicate()[0]

nothing gets printed out (even for p.communicate()[1])

I think the problem is the quoted string the rename command wants -
when I put it in triple quotes like """s/\.htm$/\.html/""" I get some
output, but not the correct output. I've also tried escaping the
single quotes with \' and putting it in regular double quotes but that
didn't work either.

i'd appreciate any help

Mar 26 '08 #1
7 4932
also, i've tried the Shell=True parameter for Popen, but that didn't
seem to make a difference

On Mar 25, 8:31 pm, skunkwerk <skunkw...@gmai l.comwrote:
Hi,
i'm trying to call subprocess.pope n on the 'rename' function in
linux. When I run the command from the shell, like so:

rename -vn 's/\.htm$/\.html/' *.htm

it works fine... however when I try to do it in python like so:
p = subprocess.Pope n(["rename","-vn","'s/\.htm$/
\.html/'","*.htm"],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)

print p.communicate()[0]

nothing gets printed out (even for p.communicate()[1])

I think the problem is the quoted string the rename command wants -
when I put it in triple quotes like """s/\.htm$/\.html/""" I get some
output, but not the correct output. I've also tried escaping the
single quotes with \' and putting it in regular double quotes but that
didn't work either.

i'd appreciate any help
Mar 26 '08 #2
En Wed, 26 Mar 2008 00:39:05 -0300, skunkwerk <sk*******@gmai l.com>
escribió:
> i'm trying to call subprocess.pope n on the 'rename' function in
linux. When I run the command from the shell, like so:

rename -vn 's/\.htm$/\.html/' *.htm

it works fine... however when I try to do it in python like so:
p = subprocess.Pope n(["rename","-vn","'s/\.htm$/
\.html/'","*.htm"],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)

print p.communicate()[0]

nothing gets printed out (even for p.communicate()[1])
I'd try with:

p = subprocess.Pope n(["rename", "-vn", r"'s/\.htm$/\.html/'", "*.htm"],
stdout=subproce ss.PIPE, stderr=subproce ss.PIPE,
shell=True)

(note that I added shell=True and I'm using a raw string to specify the
reg.expr.)

--
Gabriel Genellina

Mar 26 '08 #3
On Mar 25, 9:25*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Wed, 26 Mar 2008 00:39:05 -0300, skunkwerk <skunkw...@gmai l.com*
escribió:
* *i'm trying to call subprocess.pope n on the 'rename' function in
linux. *When I run the command from the shell, like so:
rename -vn 's/\.htm$/\.html/' *.htm
it works fine... however when I try to do it in python like so:
p = subprocess.Pope n(["rename","-vn","'s/\.htm$/
\.html/'","*.htm"],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)
print p.communicate()[0]
nothing gets printed out (even for p.communicate()[1])

I'd try with:

p = subprocess.Pope n(["rename", "-vn", r"'s/\.htm$/\.html/'", "*.htm"],
* * * *stdout=subproc ess.PIPE, stderr=subproce ss.PIPE,
* * * *shell=True)

(note that I added shell=True and I'm using a raw string to specify the *
reg.expr.)

--
Gabriel Genellina
Thanks Gabriel,
I tried the new command and one with the raw string and single
quotes, but it is still giving me the same results (no output). any
other suggestions?

cheers
Mar 26 '08 #4
En Wed, 26 Mar 2008 02:15:28 -0300, skunkwerk <sk*******@gmai l.com>
escribió:
On Mar 25, 9:25*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
>En Wed, 26 Mar 2008 00:39:05 -0300, skunkwerk <skunkw...@gmai l.com*
escribió:
>* *i'm trying to call subprocess.pope n on the 'rename' function in
linux. *When I run the command from the shell, like so:
>rename -vn 's/\.htm$/\.html/' *.htm
>it works fine... however when I try to do it in python like so:
p = subprocess.Pope n(["rename","-vn","'s/\.htm$/
\.html/'","*.htm"],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)
>print p.communicate()[0]
>nothing gets printed out (even for p.communicate()[1])

I'd try with:

p = subprocess.Pope n(["rename", "-vn", r"'s/\.htm$/\.html/'", "*.htm"],
* * * *stdout=subproc ess.PIPE, stderr=subproce ss.PIPE,
* * * *shell=True)

(note that I added shell=True and I'm using a raw string to specify the
reg.expr.)

Thanks Gabriel,
I tried the new command and one with the raw string and single
quotes, but it is still giving me the same results (no output). any
other suggestions?
My next try would be without the single quotes...

--
Gabriel Genellina

Mar 26 '08 #5
On Mar 25, 11:04*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Wed, 26 Mar 2008 02:15:28 -0300, skunkwerk <skunkw...@gmai l.com*
escribió:
On Mar 25, 9:25*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Wed, 26 Mar 2008 00:39:05 -0300, skunkwerk <skunkw...@gmai l.com*
escribió:
* *i'm trying to call subprocess.pope n on the 'rename' function in
linux. *When I run the command from the shell, like so:
rename -vn 's/\.htm$/\.html/' *.htm
it works fine... however when I try to do it in python like so:
p = subprocess.Pope n(["rename","-vn","'s/\.htm$/
\.html/'","*.htm"],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)
print p.communicate()[0]
nothing gets printed out (even for p.communicate()[1])
I'd try with:
p = subprocess.Pope n(["rename", "-vn", r"'s/\.htm$/\.html/'", "*.htm"],
* * * *stdout=subproc ess.PIPE, stderr=subproce ss.PIPE,
* * * *shell=True)
(note that I added shell=True and I'm using a raw string to specify the *
reg.expr.)
Thanks Gabriel,
* *I tried the new command and one with the raw string and single
quotes, but it is still giving me the same results (no output). *any
other suggestions?

My next try would be without the single quotes...

--
Gabriel Genellina
thanks for the input guys,
I've tried the suggestions but can't get it to work. I have a file
named test.htm in my directory, and when I run the following command:

rename -vn 's/(.*)\.htm$/model.html/' *.htm

from the shell in that directory I get the following output:
test.htm renamed as model.html

now my python script is called test.py, is located in the same
directory, and is called from the shell with 'python test.py'
the contents of test.py:
import subprocess

p = subprocess.Pope n(['rename','-vn','s/(.*)\.htm$/
model.html/','*.htm'],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)
print p.communicate()[0]

i change to print p.communicate()[1] in case the output is blank the
first time

this is the output:
*.htm renamed as model.html

when I add shell=True to the subprocess command, I get the following
output:
Usage: rename [-v] [-n] [-f] perlexpr [filenames]

am i doing something wrong?
Mar 26 '08 #6
On Mar 26, 6:44*am, skunkwerk <skunkw...@gmai l.comwrote:
On Mar 25, 11:04*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
En Wed, 26 Mar 2008 02:15:28 -0300, skunkwerk <skunkw...@gmai l.com*
escribió:
On Mar 25, 9:25*pm, "Gabriel Genellina" <gagsl-...@yahoo.com.a r>
wrote:
>En Wed, 26 Mar 2008 00:39:05 -0300, skunkwerk <skunkw...@gmai l.com*
>escribió:
>* *i'm trying to call subprocess.pope n on the 'rename' function in
>linux. *When I run the command from the shell, like so:
>rename -vn 's/\.htm$/\.html/' *.htm
>it works fine... however when I try to do it in python like so:
>p = subprocess.Pope n(["rename","-vn","'s/\.htm$/
>\.html/'","*.htm"],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)
>print p.communicate()[0]
>nothing gets printed out (even for p.communicate()[1])
>I'd try with:
>p = subprocess.Pope n(["rename", "-vn", r"'s/\.htm$/\.html/'", "*.htm"],
>* * * *stdout=subproc ess.PIPE, stderr=subproce ss.PIPE,
>* * * *shell=True)
>(note that I added shell=True and I'm using a raw string to specifythe *
>reg.expr.)
Thanks Gabriel,
* *I tried the new command and one with the raw string and single
quotes, but it is still giving me the same results (no output). *any
other suggestions?
My next try would be without the single quotes...
--
Gabriel Genellina

thanks for the input guys,
* I've tried the suggestions but can't get it to work. *I have a file
named test.htm in my directory, and when I run the following command:

rename -vn 's/(.*)\.htm$/model.html/' *.htm

from the shell in that directory I get the following output:
test.htm renamed as model.html

now my python script is called test.py, is located in the same
directory, and is called from the shell with 'python test.py'
the contents of test.py:
import subprocess

p = subprocess.Pope n(['rename','-vn','s/(.*)\.htm$/
model.html/','*.htm'],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)
print p.communicate()[0]

i change to print p.communicate()[1] in case the output is blank the
first time

this is the output:
*.htm renamed as model.html

when I add shell=True to the subprocess command, I get the following
output:
Usage: rename [-v] [-n] [-f] perlexpr [filenames]

am i doing something wrong?
in addition, when I use Popen without any quotes, or without quotes
for the regular expression, I get an exception.

I'm running ubuntu linux 7.10 with python 2.5.1

thanks
Mar 26 '08 #7
On Mar 26, 10:33*pm, skunkwerk <skunkw...@gmai l.comwrote:
On Mar 26, 8:05*am, Jeffrey Froman <jeff...@fro.ma nwrote:
skunkwerkwrote:
p = subprocess.Pope n(['rename','-vn','s/(.*)\.htm$/
model.html/','*.htm'],stdout=subproc ess.PIPE,stderr =subprocess.PIP E)
print p.communicate()[0]
i change to print p.communicate()[1] in case the output is blank the
first time
this is the output:
*.htm renamed as model.html
Without shell=True, your glob characters will not be expanded. Hence, the
command looks for a file actually named "*.htm"
when I add shell=True to the subprocess command, I get the following
output:
Usage: rename [-v] [-n] [-f] perlexpr [filenames]
Here the use of the shell may be confounding the arguments passed. Your
command will probably work better if you avoid using shell=True. However,
you will need to perform your own globbing:
# Untested (no perl-rename here):
command = ['rename','-vn', 's/(.*)\.htm$/model.html/']
files = glob.glob('*.ht m')
command.extend( files)
p = subprocess.Pope n(
* * command,
* * stdout=subproce ss.PIPE,
* * stderr=subproce ss.PIPE,
* * )
Jeffrey

thanks Jeffrey, that worked like a charm!
I'm trying to detect when the subprocess has terminated using the
wait() function - but when there is an error with the call to rename
(ie the file doesn't exist) rename (when run from the command line
just terminates and displays the error). In the code above, though,
my call to p.wait() just hangs when rename should throw an error...
I've tried adding shell=True but that stops the rename from working.
any ideas?

thanks
Jun 27 '08 #8

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

Similar topics

5
8655
by: Cameron Laird | last post by:
Question: import subprocess, StringIO input = StringIO.StringIO("abcdefgh\nabc\n") # I don't know of a compact, evocative, and # cross-platform way to exhibit this behavior. # For now, depend on cat(1). p = subprocess.Popen(, stdout = subprocess.PIPE, stdin = response)
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
9
6495
by: Phoe6 | last post by:
Hi all, Consider this scenario, where in I need to use subprocess to execute a command like 'ping 127.0.0.1' which will have a continuous non- terminating output in Linux. # code # This hangs at this point. How should I handle these kind of commands (ping 127.0.0.1) with
3
1867
by: mclaugb | last post by:
Hello ALl, I have a compiled program "conv.exe" that works as follows: ----------------------------- Please selection from the following options. press "h" for help, "p" for print, "r" for readfile. Enter your request now: .... -------------------- Is there a way to script python using the subprocess method to start this program "conv.exe" and then send a "r" to the command line to make it, say,
3
6025
by: BartlebyScrivener | last post by:
Using bash on Debian Etch. If word_doc = sys.argv 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...
1
5090
by: Steven Bethard | last post by:
I'm having trouble using the subprocess module on Windows when my command line includes special characters like "&" (ampersand):: .... stdout=subprocess.PIPE, .... stderr=subprocess.PIPE) "'y' is not recognized as an internal or external command,\r\noperable program or batch file.\r\n" As you can see, Windows is interpreting that "&" as separating two commands, instead of being part of the single argument...
8
4211
by: clyfish | last post by:
In cmd, I can use find like this. C:\>netstat -an | find "445" TCP 0.0.0.0:445 0.0.0.0:0 LISTENING UDP 0.0.0.0:445 *:* C:\> And os.system is OK. TCP 0.0.0.0:445 0.0.0.0:0 LISTENING
25
2792
by: Jeremy Banks | last post by:
Hi. I wondered if anyone knew the rationale behind the naming of the Popen class in the subprocess module. Popen sounds like the a suitable name for a function that created a subprocess, but the object itself is a subprocess, not a "popen". It seems that it would be more accurate to just name the class Subprocess, can anyone explain why this is not the case? Thank you.
1
8675
by: Mark Shewfelt | last post by:
Hello, I am attempting to use Popen() in a Windows service. I have a small Win32 .exe that I normally run through the os.popen2() function. I've written a class to work with the input and output parameters that are passed and captured from this exe. When I use the class outside of a service using either subprocess.Popen or os.popen2 work just fine. When I use this class inside a Windows service it doesn't work. It doesn't crash the...
0
10153
Oralloy
by: Oralloy | last post by:
Hello folks, I am unable to find appropriate documentation on the type promotion of bit-fields when using the generalised comparison operator "<=>". The problem is that using the GNU compilers, it seems that the internal comparison operator "<=>" tries to promote arguments from unsigned to signed. This is as boiled down as I can make it. Here is my compilation command: g++-12 -std=c++20 -Wnarrowing bit_field.cpp Here is the code in...
0
10007
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
9946
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
8830
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...
0
6646
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 then checking html paragraph one by one. At the time of converting from word file to html my equations which are in the word document file was convert into image. Globals.ThisAddIn.Application.ActiveDocument.Select();...
0
5272
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
5413
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
2
3530
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
3
2800
bsmnconsultancy
by: bsmnconsultancy | last post by:
In today's digital era, a well-designed website is crucial for businesses looking to succeed. Whether you're a small business owner or a large corporation in Toronto, having a strong online presence can significantly impact your brand's success. BSMN Consultancy, a leader in Website Development in Toronto offers valuable insights into creating effective websites that not only look great but also perform exceptionally well. In this comprehensive...

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.