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

executing bash scripts sequentially in python

rhitam30111985
100+
P: 112
Hi all again . didnt know whether to put this in linux forum or here ... anyway.. i am trying to execute a sequence of bash commands from python .. but it doesnt seem to work :

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. import os 
  3. os.system("myfile=/home/pythonprac/commonwords")
  4. os.system("echo $myfile")
  5.  
this is giving a blank output whereas it is supposed to display /home/pythonprac/commonwords

any idea where i am going wrong ?

thanks
Apr 9 '08 #1
Share this Question
Share on Google+
6 Replies


jlm699
100+
P: 314
I believe that once the first os.system call ends any environment variables you may have created get cleaned up. Another solution would be to instead of executing individual commands write those commands to some temporary batch file. Then execute only the file. HTH
Apr 9 '08 #2

P: 32
Hi all again . didnt know whether to put this in linux forum or here ... anyway.. i am trying to execute a sequence of bash commands from python .. but it doesnt seem to work :

Expand|Select|Wrap|Line Numbers
  1. #!/usr/bin/python
  2. import os 
  3. os.system("myfile=/home/pythonprac/commonwords")
  4. os.system("echo $myfile")
  5.  
this is giving a blank output whereas it is supposed to display /home/pythonprac/commonwords

any idea where i am going wrong ?

thanks
Its best to use the 'popen' method ....

example of looking at whats in a text file on your system:

Expand|Select|Wrap|Line Numbers
  1. from os import popen
  2.  
  3. file = <some_file_on_your_system>
  4. cmd = ('cat %s' % file)
  5. ff = popen(cmd)
  6. ff.readlines()
  7.  
  8.  
Hope this helps .... write back if you need further input
Apr 9 '08 #3

Expert 100+
P: 849
You can also use a semicolon after a command to run a second command (or sequence of commands) on the shell. For example:
Expand|Select|Wrap|Line Numbers
  1. import os
  2. os.system('ls; cd ..')
  3.  
will run ls and then change to the parent directory.
Apr 9 '08 #4

Expert 100+
P: 511
you don't have to call bash commands for what you are doing, reading a file?
use Python's own methods. this will ensure portability with your code
Expand|Select|Wrap|Line Numbers
  1. myfile="/home/myfile"
  2. for lines in open(myfile): # equivalent of unix cat (or other tools for file reading)
  3.     print lines
  4.  
if you want to use ls, use Python's os.listdir, glob.glob(), etc
if you want to cd, use os.chdir() etc
Look at the os module (as well as sys) for such things.
Apr 9 '08 #5

rhitam30111985
100+
P: 112
you don't have to call bash commands for what you are doing, reading a file?
use Python's own methods. this will ensure portability with your code
Expand|Select|Wrap|Line Numbers
  1. myfile="/home/myfile"
  2. for lines in open(myfile): # equivalent of unix cat (or other tools for file reading)
  3.     print lines
  4.  
if you want to use ls, use Python's os.listdir, glob.glob(), etc
if you want to cd, use os.chdir() etc
Look at the os module (as well as sys) for such things.
thanks a ton everyone . .. actually i was trying notjust to read a file but mail the contents of the file to any email address using the mail comamnd of bash . ... because for some reason SMTPlib was not working . anyhow ... . problem solved :-)
Apr 10 '08 #6

P: 32
thanks a ton everyone . .. actually i was trying notjust to read a file but mail the contents of the file to any email address using the mail comamnd of bash . ... because for some reason SMTPlib was not working . anyhow ... . problem solved :-)
Would you mind posting your solution .... just for completeness ...

Thanks,
Apr 10 '08 #7

Post your reply

Sign in to post your reply or Sign up for a free account.