By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
440,091 Members | 1,555 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 440,091 IT Pros & Developers. It's quick & easy.

Can we pass some arguments to system("cmdline")?

P: n/a
Hi!
I was wondering if we can pass some arguments to system("cmdline")?

E.g in Perl, we can do something like:

$dir="/home/cypher";

system("ls $dir");

which would instruct Perl to do an "ls /home/cypher"

But in python, doing something like

dir="/home/cypher"
system("ls dir")

would cause python to execute "ls dir" where "dir" might not exist at
all! Is there a way to reproduce the same thing in Python?

Thanks for any insights.

cheers,
Didier.

Jul 19 '05 #1
Share this Question
Share on Google+
5 Replies


P: n/a
Didier C wrote:
Hi!
I was wondering if we can pass some arguments to system("cmdline")?

E.g in Perl, we can do something like:

$dir="/home/cypher";

system("ls $dir");

which would instruct Perl to do an "ls /home/cypher"

But in python, doing something like

dir="/home/cypher"
system("ls dir")

would cause python to execute "ls dir" where "dir" might not exist at
all! Is there a way to reproduce the same thing in Python?

Thanks for any insights.

cheers,
Didier.


You should use something like this:

dir = "/home/cypher"
system("ls %s" % dir)

--
Qiangning Hong

_______________________________________________
/ lp1 on fire \
| |
\ -- One of the more obfuscated kernel messages /
-----------------------------------------------
\
\
\
___ _____ ___
/ \ / /| / \
| | / / | | |
| | /____/ | | |
| | | | | | |
| | | {} | / | |
| | |____|/ | |
| | |==| | |
| \___________/ |
| |
| |
Jul 19 '05 #2

P: n/a
On Sun, Jun 19, 2005 at 11:12:05PM -0700, Didier C wrote:
Hi!
I was wondering if we can pass some arguments to system("cmdline")?

E.g in Perl, we can do something like:

$dir="/home/cypher";

system("ls $dir");

which would instruct Perl to do an "ls /home/cypher"

But in python, doing something like

dir="/home/cypher"
system("ls dir")

system("ls %(dir)s" % locals())
system("ls %s" % dir)
system("ls %(name)s" % dict(name=dir)

But you should consider if you really really want to do this.
What happens when " " in dir?
What happens when dir == "; rm -Rf /"

Andreas
Jul 19 '05 #3

P: n/a
Didier C wrote:
E.g in Perl, we can do something like:

$dir="/home/cypher";

system("ls $dir");

Is there a way to reproduce the same thing in Python?


system("ls %s" % dir)

But you should really be using subprocess for security (so that if
dir=="/home/foo; rm -rf /" nothing bad will happen):

import subprocess
subprocess.Popen(['ls', dir])
Jul 19 '05 #4

P: n/a
Thanks all for the reply. I'll try out those things out. :)

Cheers,
Didier.
Leif K-Brooks a écrit :
Didier C wrote:
E.g in Perl, we can do something like:

$dir="/home/cypher";

system("ls $dir");

Is there a way to reproduce the same thing in Python?


system("ls %s" % dir)

But you should really be using subprocess for security (so that if
dir=="/home/foo; rm -rf /" nothing bad will happen):

import subprocess
subprocess.Popen(['ls', dir])


Jul 19 '05 #5

P: n/a
>>>>> "Didier Casse" <el********@gmail.com> (DC) wrote:
DC> Thanks all for the reply. I'll try out those things out. :)


But don't try the following
> system("ls $dir"); with dir=="/home/foo; rm -rf /"


--
Piet van Oostrum <pi**@cs.uu.nl>
URL: http://www.cs.uu.nl/~piet [PGP]
Private email: pi**@vanoostrum.org
Jul 19 '05 #6

This discussion thread is closed

Replies have been disabled for this discussion.