How to do regular BASH work in Python?

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

    How to do regular BASH work in Python?

    What is the best way to do the regular bash commands in native python?

    - create directory
    - create file
    - make a symlink
    - copy a file to another directory
    - move a file
    - set permissions

    I need to write a program that creates real application/FTP accounts
    and make regular backups to external disk and I think its best to do
    it all consistently with my application in Python.
    This way I can easily link it to the customer database and front-end
    web application. I'd want to avoid BASH/SHELL if that's possible.
  • Frantisek Malina

    #2
    Re: How to do regular BASH work in Python?

    Hey,
    I found it. Python rocks:


    If you have any further links that provide some lively code examples
    and recipes, please pass them on.

    Thank you

    Frank Malina

    Comment

    • jdd

      #3
      Re: How to do regular BASH work in Python?

      On Oct 9, 10:13 am, Frantisek Malina <fmal...@gmail. comwrote:
      Hey,
      I found it. Python rocks:http://www.python.org/doc/2.5.2/lib/os-file-dir.html
      >
      If you have any further links that provide some lively code examples
      and recipes, please pass them on.
      >
      Thank you
      >
      Frank Malinahttp://vizualbod.com
      http://examples.oreilly.com/python3/ has some good examples, although
      they may be a little tough to read through if you don't have a copy of
      "programmin g python", the book they're from.

      Comment

      • Frank Malina @ vizualbod.com

        #4
        Re: How to do regular BASH work in Python?

        On Oct 9, 3:22 pm, jdd <jeremiah.do... @gmail.comwrote :
        On Oct 9, 10:13 am, Frantisek Malina <fmal...@gmail. comwrote:
        >>
        If you have any further links that provide some lively code examples
        and recipes, please pass them on.
        >
        Thank you
        >
        Frank Malinahttp://vizualbod.com
        >
        http://examples.oreilly.com/python3/has some good examples, although
        they may be a little tough to read through if you don't have a copy of
        "programmin g python", the book they're from.
        Coming form PHP, I find Python is pleasure to read no matter what.

        Comment

        • Chris Rebert

          #5
          Re: How to do regular BASH work in Python?

          You might also be interested in the 'shutil' module:
          Source code: Lib/shutil.py The shutil module offers a number of high-level operations on files and collections of files. In particular, functions are provided which support file copying and removal...


          Cheers,
          Chris
          --
          Follow the path of the Iguana...


          On Thu, Oct 9, 2008 at 7:13 AM, Frantisek Malina <fmalina@gmail. comwrote:
          Hey,
          I found it. Python rocks:

          >
          If you have any further links that provide some lively code examples
          and recipes, please pass them on.
          >
          Thank you
          >
          Frank Malina

          --

          >

          Comment

          • Michael Torrie

            #6
            Re: How to do regular BASH work in Python?

            Frantisek Malina wrote:
            What is the best way to do the regular bash commands in native python?
            >
            - create directory
            - create file
            - make a symlink
            - copy a file to another directory
            - move a file
            - set permissions
            >
            I need to write a program that creates real application/FTP accounts
            and make regular backups to external disk and I think its best to do
            it all consistently with my application in Python.
            This way I can easily link it to the customer database and front-end
            web application. I'd want to avoid BASH/SHELL if that's possible.
            As others have said, the os and shutils modules should fulfill most of
            your needs.

            But unfortunately Python is definitely not a shell-scripting language
            and some things that are easy in bash are really, really hard in python,
            such as interacting with and stringing together processes, redirecting
            outputs, etc. Whereas in bash it's easy to create pipes between
            processes and redirect inputs with '|', '>', '2>', etc, in Python you'd
            have to use the subprocess module to spawn a real shell to do much of
            that.

            In many circumstances, though, the purpose of the piping and redirection
            in Bash is to do text processing, which is very easy to do with python
            and generators. See http://www.dabeaz.com/generators/ for the real
            power that python can bear on the problems traditionally solved with
            bash. I find that in most cases in python, a simple wrapper around
            subprocess to run a command and get its stdout, stderr and return error
            code works pretty well.

            Comment

            • Lawrence D'Oliveiro

              #7
              Re: How to do regular BASH work in Python?

              In message <mailman.2324.1 223656381.3487. python-list@python.org >, Michael
              Torrie wrote:
              But unfortunately Python is definitely not a shell-scripting language
              and some things that are easy in bash are really, really hard in
              python ...

              Why not combine the two
              <http://groups.google.c o.nz/group/nz.comp/msg/3a572e757bbdd81 6>.

              Comment

              Working...