executing bash scripts sequentially in python

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • rhitam30111985
    New Member
    • Aug 2007
    • 112

    executing bash scripts sequentially in python

    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 :

    [CODE=python]
    #!/usr/bin/python
    import os
    os.system("myfi le=/home/pythonprac/commonwords")
    os.system("echo $myfile")
    [/CODE]

    this is giving a blank output whereas it is supposed to display /home/pythonprac/commonwords

    any idea where i am going wrong ?

    thanks
  • jlm699
    Contributor
    • Jul 2007
    • 314

    #2
    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

    Comment

    • fordie1000
      New Member
      • Mar 2008
      • 32

      #3
      Originally posted by rhitam30111985
      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 :

      [CODE=python]
      #!/usr/bin/python
      import os
      os.system("myfi le=/home/pythonprac/commonwords")
      os.system("echo $myfile")
      [/CODE]

      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:

      Code:
      from os import popen
      
      file = <some_file_on_your_system>
      cmd = ('cat %s' % file)
      ff = popen(cmd)
      ff.readlines()
      Hope this helps .... write back if you need further input

      Comment

      • Laharl
        Recognized Expert Contributor
        • Sep 2007
        • 849

        #4
        You can also use a semicolon after a command to run a second command (or sequence of commands) on the shell. For example:
        [CODE=python]
        import os
        os.system('ls; cd ..')
        [/CODE] will run ls and then change to the parent directory.

        Comment

        • ghostdog74
          Recognized Expert Contributor
          • Apr 2006
          • 511

          #5
          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
          Code:
          myfile="/home/myfile"
          for lines in open(myfile): # equivalent of unix cat (or other tools for file reading)
              print lines
          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.

          Comment

          • rhitam30111985
            New Member
            • Aug 2007
            • 112

            #6
            Originally posted by ghostdog74
            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
            Code:
            myfile="/home/myfile"
            for lines in open(myfile): # equivalent of unix cat (or other tools for file reading)
                print lines
            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 :-)

            Comment

            • fordie1000
              New Member
              • Mar 2008
              • 32

              #7
              Originally posted by rhitam30111985
              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,

              Comment

              Working...