Command line

Fredrik Lundh fredrik at pythonware.com
Sun Nov 20 16:19:36 EST 2005


"amfr" wrote:

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

os.system(cmd), or some relative:

    http://docs.python.org/lib/os-process.html
    http://docs.python.org/lib/os-newstreams.html#os-newstreams

or

    http://docs.python.org/lib/module-subprocess.html

:::

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(directory)
    for file in files:
        file = os.path.join(directory, file) # full path
        ...

or

    files = glob.glob(pattern)
    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.getmtime(file)
    (etc)

</F>






More information about the Python-list mailing list