473,386 Members | 2,129 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,386 software developers and data experts.

Using os.system() and string concatenation

Using Python 2.3

Currently I process three files and build the output of those files in
to lists using for statements.

I think take those lists and provide them to an os.system() call.

cmd = "/usr/sbin/useradd"
os.system(cmd + list1[0] + list1[1] + list2[0] + list3[0])

This executes without any errors, but doesn't execute the command
supplied to os.system(). Now if I place them all in cmd first, then
supply it to os.system() it executes just fine. So there really isn't
a problem, just wanting to know what would cause such behavior.

Thanks,

Wayne
Jul 18 '05 #1
1 3539
In article <ma**************************************@python.o rg>,
Wayne Witzel III <ww******@gmail.com> wrote:
Using Python 2.3

Currently I process three files and build the output of those files in
to lists using for statements.

I think take those lists and provide them to an os.system() call.

cmd = "/usr/sbin/useradd"
os.system(cmd + list1[0] + list1[1] + list2[0] + list3[0])

This executes without any errors, but doesn't execute the command
supplied to os.system(). Now if I place them all in cmd first, then
supply it to os.system() it executes just fine. So there really isn't
a problem, just wanting to know what would cause such behavior.


As already mentioned in other followups, could be white
space missing.

On the other hand, this looks to me like a very good place to
use os.spawnv instead of os.system. It might resolve the
present problem, but much more importantly, it will avoid
more dangerous problems of a similar nature. When you construct
a shell command out of data from files, data becomes shell
syntax, and there is in theory the possibility that the result
will be worse than just invalid, it may execute a different
command or different parameters than you had in mind, with a
wide range of potentially unpleasant results.

But spawnv() executes the command directly, with the parameters
you supply, unlike system() which invokes the shell. So with
spawnv() you don't need white space, but more importantly the
only command that can run as a result is the one you specify.

os.spawnv(os.P_WAIT, '/usr/sbin/useradd',
['useradd', list1[0], list1[1], list2[0], list3[0]])

Donn Cave, do**@u.washington.edu
Jul 18 '05 #2

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

Similar topics

5
by: Jonas Galvez | last post by:
Is it true that joining the string elements of a list is faster than concatenating them via the '+' operator? "".join() vs 'a'+'b'+'c' If so, can anyone explain why?
20
by: hagai26 | last post by:
I am looking for the best and efficient way to replace the first word in a str, like this: "aa to become" -> "/aa/ to become" I know I can use spilt and than join them but I can also use regular...
11
by: Dorsa | last post by:
HI, Could you please tell me the error in here. I am trying to open an XML file from a link. Response.Clear() Response.Expires = 0 Response.BufferOutput = False Response.ContentType =...
23
by: Bonj | last post by:
what is the correct form of string concatenation in VB.NET, + or &? Both seem to work, but which is correct? I know it's + in C# and & in VB6, but which in VB.NET?
4
by: FB's .NET Dev PC | last post by:
Interesting note, the code below as is will attempt to cast what is clearly indicated as a string into a double. This is becuase the use of + as a concatenation operator. The error message...
3
by: Mythran | last post by:
Out of curiosity, only, which is recommended for SHORT concatenation...or concatenating two or three strings that are relatively small in size? Dim a As String = "bah" Dim b As String = "bah2"...
9
by: Justin M. Keyes | last post by:
Hi, Please read carefully before assuming that this is the same old question about string concatenation in C#! It is well-known that the following concatenation produces multiple immutable...
34
by: Larry Hastings | last post by:
This is such a long posting that I've broken it out into sections. Note that while developing this patch I discovered a Subtle Bug in CPython, which I have discussed in its own section below. ...
17
by: R.Rafii | last post by:
Hi, I have a simple (?) question for you all experts. I have a button that performs a query on my SQL and fill a datagrid on the form The code: Dim sconn As New SqlConnection()...
1
by: Tony Johansson | last post by:
Hello! Everything not derived from System.ValueType is a reference type. I just wonder this ValueType type is derived from Object and it overrides the two methods Equals and GetHashCode which...
0
by: taylorcarr | last post by:
A Canon printer is a smart device known for being advanced, efficient, and reliable. It is designed for home, office, and hybrid workspace use and can also be used for a variety of purposes. However,...
0
by: ryjfgjl | last post by:
If we have dozens or hundreds of excel to import into the database, if we use the excel import function provided by database editors such as navicat, it will be extremely tedious and time-consuming...
0
by: ryjfgjl | last post by:
In our work, we often receive Excel tables with data in the same format. If we want to analyze these data, it can be difficult to analyze them because the data is spread across multiple Excel files...
0
by: emmanuelkatto | last post by:
Hi All, I am Emmanuel katto from Uganda. I want to ask what challenges you've faced while migrating a website to cloud. Please let me know. Thanks! Emmanuel
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
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,...
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...

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.