Command line

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • amfr

    #1

    Command line

    Hoe would I call something on the command line from python, e.g. "ls
    -la"?

  • avnit

    #2
    Re: Command line

    What OS are you using. I'm not sure about anything else but on a mac
    it's:
    [color=blue][color=green]
    >>import os
    >>os.system(" ls -la")[/color][/color]

    Comment

    • Diez B. Roggisch

      #3
      Re: Command line

      amfr wrote:[color=blue]
      > Hoe would I call something on the command line from python, e.g. "ls
      > -la"?[/color]

      Use the module subprocess - otherwise maybe popen2 or os.

      Regards,

      Diez

      Comment

      • avnit

        #4
        Re: Command line

        What OS are you using. I'm not sure about anything else but on a mac
        it's:
        [color=blue][color=green]
        >>import os
        >>os.system(" ls -la")[/color][/color]

        Comment

        • Diez B. Roggisch

          #5
          Re: Command line

          amfr wrote:[color=blue]
          > Hoe would I call something on the command line from python, e.g. "ls
          > -la"?[/color]

          Use the module subprocess - otherwise maybe popen2 or os.

          Regards,

          Diez

          Comment

          • Fredrik Lundh

            #6
            Re: Command line

            "amfr" wrote:
            [color=blue]
            > Hoe would I call something on the command line from python, e.g. "ls
            > -la"?[/color]

            os.system(cmd), or some relative:




            or

            Source code: Lib/subprocess.py The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace seve...


            :::

            also note that you shouldn't use external commands for trivial things like
            getting a list of files or getting the size of a file; you can get a list of all
            files in a directory with

            files = os.listdir(dire ctory)
            for file in files:
            file = os.path.join(di rectory, file) # full path
            ...

            or

            files = glob.glob(patte rn)
            for file in files:
            ...

            and you can get information about a given file with

            st = os.stat(file)

            size = st.st_size
            mtime = st.st_mtime
            (etc)

            or

            size = os.path.getsize (file)
            mtime = os.path.getmtim e(file)
            (etc)

            </F>



            Comment

            • Fredrik Lundh

              #7
              Re: Command line

              "amfr" wrote:
              [color=blue]
              > Hoe would I call something on the command line from python, e.g. "ls
              > -la"?[/color]

              os.system(cmd), or some relative:




              or

              Source code: Lib/subprocess.py The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace seve...


              :::

              also note that you shouldn't use external commands for trivial things like
              getting a list of files or getting the size of a file; you can get a list of all
              files in a directory with

              files = os.listdir(dire ctory)
              for file in files:
              file = os.path.join(di rectory, file) # full path
              ...

              or

              files = glob.glob(patte rn)
              for file in files:
              ...

              and you can get information about a given file with

              st = os.stat(file)

              size = st.st_size
              mtime = st.st_mtime
              (etc)

              or

              size = os.path.getsize (file)
              mtime = os.path.getmtim e(file)
              (etc)

              </F>



              Comment

              • Bengt Richter

                #8
                Re: Command line

                On Mon, 21 Nov 2005 19:39:42 -0500, Peter Hansen <peter@engcorp. com> wrote:
                [color=blue]
                >amfr wrote:[color=green]
                >> Thanks for your help. Another question, is there an built in md5/sha1
                >> function in python?[/color]
                >
                >Yes.
                >
                >Although it's a long list, it is worthwhile as a newbie for one to
                >peruse the list of standard modules from time to time, until you gain a
                >familiarity with what is there.
                >
                >http://docs.python.org/modindex.html
                >[/color]
                Worthwhile as oldbies too ;-)

                Regards,
                Bengt Richter

                Comment

                Working...