473,756 Members | 7,560 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

shell command needs whitespace characters escaped

I downloaded a file which has a space in the filename. I want to run a
shell unzip on it, but it fails in my current code:

syscmd = "cd %s ; unzip %s" % (self.storage.i nput,
file.basename() )
os.system(syscm d)

because no escaping was done.

Is there a more principled way to construct a shell command and execute
it so that all necessary characters are escaped?

Dec 8 '06 #1
3 4792
Metaperl wrote:
I downloaded a file which has a space in the filename. I want
to run a shell unzip on it, but it fails in my current code:

syscmd = "cd %s ; unzip %s" % (self.storage.i nput, file.basename() )
os.system(syscm d)

because no escaping was done.
Using "cd '%s' ; unzip '%s'" as your formatting string should work.

Cheers,

--
Klaus Alexander Seistrup
http://klaus.seistrup.dk/
Dec 8 '06 #2
metaperl wrote:
I downloaded a file which has a space in the filename. I want to run a
shell unzip on it, but it fails in my current code:

syscmd = "cd %s ; unzip %s" % (self.storage.i nput,
file.basename() )
os.system(syscm d)

because no escaping was done.

Is there a more principled way to construct a shell command and execute
it so that all necessary characters are escaped?
use subprocess.list 2cmdline to create the command string (or better,
subprocess.call ). see this FAQ entry for sample code:

http://effbot.org/pyfaq/why-can-t-ra...-backslash.htm

</F>

Dec 8 '06 #3
Fredrik Lundh <fr*****@python ware.comwrote:
metaperl wrote:
I downloaded a file which has a space in the filename. I want to run a
shell unzip on it, but it fails in my current code:

syscmd = "cd %s ; unzip %s" % (self.storage.i nput, file.basename() )
os.system(syscm d)

because no escaping was done.

Is there a more principled way to construct a shell command and execute
it so that all necessary characters are escaped?

use subprocess.list 2cmdline to create the command string
That is windows only isn't it?
>>subprocess.li st2cmdline(['a', "'b", 'c'])
"a 'b c"

Doesn't make a unix shell friendly command line.

For unix you can use this

def quotemeta(args) :
"""Quote all the metacharacters in the args for the unix shell"""
return " ".join([re.sub(r"([^A-Za-z0-9_])", r"\\\1", string) for string in args])
>>print quotemeta(['a', "'b", 'c'])
a \'b c
(or better, subprocess.call ).
A good idea!

--
Nick Craig-Wood <ni**@craig-wood.com-- http://www.craig-wood.com/nick
Dec 8 '06 #4

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

Similar topics

10
5248
by: Jack | last post by:
I'm trying to make a script that will search a list of files and remove the following line <meta http-equiv="Content-Type" content="text/html; charset=<?php echo CHARSET; ?>"> I'm doing this by generating a sed command and using system to execute it like so: $t = sprintf("%s \"s/<meta http-equiv="Content-Type" content="text/html;
3
8256
by: John Bowling | last post by:
I have a java (2.0) program with the following lines: String cmdArray1 = {"lp", "-d", "hp4m", "MyFile"}; System.out.println(Runtime.getRuntime().exec(cmdArray1)); It compliles properly, but does not print the file to the printer. It displays the following as the return from Runtime...: java.lang.UNIXProcess@1034bb5
3
3485
by: Eli Criffield | last post by:
I'm try run an ssh command in pexpect and I'm having trouble getting everything escaped to do what i want. Here's a striped down script showing what i want to do. -- #!/usr/bin/env python import pexpect import sys if len(sys.argv) < 3:
8
1869
by: Dawn Minnis | last post by:
Hey guys If I have a program (see codeSnippet1) that I compile to be called test.o Then run it as test.o n n 2 3 4 I want the code to be able to strip out the two characters at the start (always going to be 2) and store them as characters. But I can't seem to get it to work because it is a pointer to a vector of characters. However, if I only run with integer arguements and use codeSnippet2 it works fine and they convert nicely to...
9
78386
by: sohan | last post by:
Hi, I want to know how to connect and execute a db2 query from inside a UNIX shell script. Details: We have a unix shell script. We need to execute multiple db2 sql queries from this shell script and export the result to a file. Any code snippet on this will be helpful.
3
5388
by: Taras_96 | last post by:
Hi everyone, I'm having a bit of trouble understanding the purpose of escaping nulls, and the use of addcslashes. Firstly, the manual states that: "Strictly speaking, MySQL requires only that backslash and the quote character used to quote the string in the query be escaped. This function quotes the other characters to make them easier to read in
12
1858
by: Torsten Bronger | last post by:
Hallöchen! I need some help with finding matches in a string that has some characters which are marked as escaped (in a separate list of indices). Escaped means that they must not be part of any match. My current approach is to look for matches in substrings with the escaped characters as boundaries between the substrings. However, then ^ and $ in the patterns are treated wrongly. (Although I use startpos and endpos parameters for...
3
10196
KevinADC
by: KevinADC | last post by:
Purpose The purpose of this article is to discuss the difference between characters inside a character class and outside a character class and some special characters inside a character class. This is not a regular expression tutorial. Assumes you are already familiar with basic regular expression concepts and terminology. If not, you may want to read some regular expression tutorial. See the end of the article for links to online resources....
5
5083
by: inetquestion | last post by:
I am looking for a web interface for shell commands or shell scripts. Does anyone know of any exexisting php scripts which would solve this requirement? PHP form accepts input from a user, then passes these as arguments to a configurable shell script or OS command. I would like for the output generated from the shell script/command shall be displayed in a new javascript window once the form is submitted. Optimally a user should not...
0
9462
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
9287
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
10046
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
9886
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...
0
8723
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
6542
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
5155
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
5318
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
3817
by: 6302768590 | last post by:
Hai team i want code for transfer the data from one system to another through IP address by using C# our system has to for every 5mins then we have to update the data what the data is updated we have to send another system

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.