473,654 Members | 3,011 Online
Bytes | Software Development & Data Engineering Community
+ Post

Home Posts Topics Members FAQ

How do I pass a variable to an external program

Hi

I would like to pass a variable to an external program i.e.

os.system('cd $variable')

However this doesnt work, I cant figure it out, any ideas?

Cheers

Rigga
Jul 18 '05 #1
5 7278
In article <sh************ ***@news-binary.blueyond er.co.uk>,
Rigga <Ri***@noemail. com> wrote:
Hi

I would like to pass a variable to an external program i.e.

os.system('c d $variable')

However this doesnt work, I cant figure it out, any ideas?

Jul 18 '05 #2
Cameron Laird wrote:
In article <sh************ ***@news-binary.blueyond er.co.uk>,
Rigga <Ri***@noemail. com> wrote:
Hi

I would like to pass a variable to an external program i.e.

os.system(' cd $variable')

However this doesnt work, I cant figure it out, any ideas?

.
.
.
In the short term,
os.system('cd %s' % variable)
is probably what you think you want.

Wow thanks for the fast response that works a treat, now i just gotta find
out how you run more than one command on a line i.e.
os.system('cd %s' 'ls -lt')
Jul 18 '05 #3
In article <gu************ ***@news-binary.blueyond er.co.uk>,
Rigga <Ri***@noemail. com> wrote:
.
.
.
In the short term,
os.system('cd %s' % variable)
is probably what you think you want.

Wow thanks for the fast response that works a treat, now i just gotta find
out how you run more than one command on a line i.e.
os.system('c d %s' 'ls -lt')


os.system('cd %s; ls -lt' % variable)

To save even more time, visit <URL: http://
www.python.org/doc/current/tut/tut.html >.
--

Cameron Laird <cl****@phaseit .net>
Business: http://www.Phaseit.net
Jul 18 '05 #4
Cameron Laird wrote:
In article <gu************ ***@news-binary.blueyond er.co.uk>,
Rigga <Ri***@noemail. com> wrote:
.
.
.
In the short term,
os.system('cd %s' % variable)
is probably what you think you want.

Wow thanks for the fast response that works a treat, now i just gotta find
out how you run more than one command on a line i.e.
os.system(' cd %s' 'ls -lt')


os.system('cd %s; ls -lt' % variable)

To save even more time, visit <URL: http://
www.python.org/doc/current/tut/tut.html >.

Thanks Cameron your help and patience has been appreciated.

Regards

Rigga
Jul 18 '05 #5
Rigga wrote:
I would like to pass a variable to an external program i.e.
There are different possiblities to that.
os.system('cd $variable')
Note that this does nothing. 'cd' only has consequences for the
current process, since it is a change directory. If you want to
change your directory, you need os.chdir instead.
See also: help(os.chdir)
However this doesnt work, I cant figure it out, any ideas?


os.system is simply given a string, where spaces mark the different
arguments. You can put a variable into a string with several
methods:

s = "cd %s" % variable
s = "cd " + str(variable)
s = "cd +%(variable)s" % locals()

You can also call os.spawn instead of os.system. This way, you
pass a list or arguments (all strings) to the program, without
the problem that spaces may occur in one of them:

os.spawn("/bin/ls", ["ls", "/usr/local"])
See also: help(os.spawn)

Please look at the online documentation at python.org for more information
on string substitution, starting subprocceses, changing directories
and other things.

Gerrit.

--
121. If any one store corn in another man's house he shall pay him
storage at the rate of one gur for every five ka of corn per year.
-- 1780 BC, Hammurabi, Code of Law
--
Asperger Syndroom - een persoonlijke benadering:
http://people.nl.linux.org/~gerrit/
Kom in verzet tegen dit kabinet:
http://www.sp.nl/

Jul 18 '05 #6

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

Similar topics

10
2534
by: Rigga | last post by:
Hi, Ok first please bear with me as I am a total Python n00b.. Can anyone explain why this does not like me using % FileLoc in the os.system call??? #!/usr/bin/python import sys import os
4
4466
by: Matthew Maylin | last post by:
I'm trying to call a external program from another. My main program is a GUI buildin borland c++ builder, and my sibbling is compiled Matlab code. I would like to call to the program, passing arguments and suppress the msdos window from popping up. I have had success with system( ) and spawnl( ) but cant seem to get the external program to just run in the background.
8
32665
by: Greg Fierro | last post by:
I would appreciate any help from anyone with the following: I have an external program (window32 based) that I am executing with the VBA SHELL command. This program produces a text file which I have to read after the external program finishes. I use the transfertext method to read the file. The problem is that the SHELL command executes external programs in an asynch way. In other words, the VBA code will continue running and NOT...
2
986
by: Aaron Bellante | last post by:
I can't find the syntax I need (I'm new to VB) to load an external program from mine. Basically, I just want to load a PDF when I click on an image in my program. Any help would be great.
4
20134
by: My SQL | last post by:
Hi Can I trigger an external program to run when a new record is added to the mysql table? The external program is in C and instead of scanning the table continuously for new insertions, it will be better if an external program could be triggered. Any suggestions?
2
1758
by: holysmokes99 | last post by:
I am developing a component in .Net 1.1, and want to debug it using the "start external program" of the debugger in the IDE. The program I want to start references both 1.1 and 2.0 components. The problem is that when I launch this from VS2003, the external program starts only for a moment and then bails out of memory with no error, and the ide returns to a stopped state awaiting my input. If I remove the 2.0 framework, at least the...
0
1376
by: jfigueiras | last post by:
>I have a problem with the module subprocess! As many other programs... I'm not sure what you mean by "non-standard file descriptors". The other program is free to open, read, write, etc any file he wants - are you trying to trap any file operation it may want to do? You *could* do such things -google for "code injection" and "API hooking"- but I doubt it's what you really want.
2
1297
by: Rich | last post by:
I want to run an external program using os.system() but I want to include a variable in the middle of the command line. An example of the type of thing I want to be able to do: pathname = os.path.dirname(sys.argv) os.system('cscript /nologo ' + pathname + '\test.vbs') When I run this, everything after pathname + seems to be ignored. Is this just incorrect syntax or am I trying to do something fundamentally wrong? Is there a better...
2
2387
by: lpcarignan | last post by:
Hi all, Right now, I'm debugging a C# library by starting an external application. To do this, I go in the project settings of the C# project, go in the Debug section and check the radio button "Start external program". I then specify the application that will start my library. I was wondering how VS 2005 does this under the cover. I'm asking this because in my situation, tons of objects from the external
0
8375
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
8290
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
8815
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
8707
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
8482
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
8593
tracyyun
by: tracyyun | last post by:
Dear forum friends, With the development of smart home technology, a variety of wireless communication protocols have appeared on the market, such as Zigbee, Z-Wave, Wi-Fi, Bluetooth, etc. Each protocol has its own unique characteristics and advantages, but as a user who is planning to build a smart home system, I am a bit confused by the choice of these technologies. I'm particularly interested in Zigbee because I've heard it does some...
0
4294
by: adsilva | last post by:
A Windows Forms form does not have the event Unload, like VB6. What one acts like?
1
1916
muto222
by: muto222 | last post by:
How can i add a mobile payment intergratation into php mysql website.
2
1593
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.