python along or bash combined with python (for manipulating files)

Discussion in 'Python' started by Peng Yu, Oct 14, 2009.

  1. Peng Yu

    Peng Yu Guest

    Bash is easy to use on manipulating files and directories (like change
    name or create links, etc) and on calling external programs. For
    simple functions, bash along is enough. However, bash does not support
    the complex functions. Python has a richer library that could provide
    support for complex functions (such compute the relative path between
    two paths).

    I'm wondering for a task that can not be done with bash along whether
    it would be better to do in pure python or with a mix of both python
    and bash. What I care is mostly coding speed and a little bit
    maintainability (but not much). Can somebody provide some experience
    on when to combine python and bash and when to use pure python?
     
    Peng Yu, Oct 14, 2009
    #1
    1. Advertising

  2. Peng Yu

    samwyse Guest

    Re: python along or bash combined with python (for manipulatingfiles)

    On Oct 13, 9:13 pm, Peng Yu <> wrote:
    > Bash is easy to use on manipulating files and directories (like change
    > name or create links, etc) and on calling external programs. For
    > simple functions, bash along is enough. However, bash does not support
    > the complex functions. Python has a richer library that could provide
    > support for complex functions (such compute the relative path between
    > two paths).
    >
    > I'm wondering for a task that can not be done with bash along whether
    > it would be better to do in pure python or with a mix of both python
    > and bash. What I care is mostly coding speed and a little bit
    > maintainability (but not much). Can somebody provide some experience
    > on when to combine python and bash and when to use pure python?


    Scripting languages try to optimize gluing disparate programs together
    to accomplish a task; bash excels at this. Programing languages try
    to optimize finding the solution to a problem; Python excels at this.

    Generally, I try to stick to one language per problem, be it bash, C+
    +, Java, Perl or Python. Bash scripts translate easily into the
    others, so you don't lose much time if you decide you started with the
    wrong language.

    Countering that, I also maintain a "toolbox" of programs that I can
    call upon when needed. In those cases, I don't hesitate to call a
    program that I've written in any language from a bash script.

    BTW, I actually prefer ksh to bash, but YMMV.
     
    samwyse, Oct 14, 2009
    #2
    1. Advertising

  3. Peng Yu

    TerryP Guest

    Re: python along or bash combined with python (for manipulatingfiles)

    On Oct 14, 2:13 am, Peng Yu <> wrote:
    > Bash is easy to use on manipulating files and directories (like change
    > name or create links, etc) and on calling external programs. For
    > simple functions, bash along is enough. However, bash does not support
    > the complex functions. Python has a richer library that could provide
    > support for complex functions (such compute the relative path between
    > two paths).
    >
    > I'm wondering for a task that can not be done with bash along whether
    > it would be better to do in pure python or with a mix of both python
    > and bash. What I care is mostly coding speed and a little bit
    > maintainability (but not much). Can somebody provide some experience
    > on when to combine python and bash and when to use pure python?


    bash can **not** manipulate files and directories beyond things like
    the '>' and '>>' I/O redirections, and some minor loading/saving of
    state data from/to files (command history, directory stack, etc). Most
    of what you refer to are **separate operating system specific
    programs** and have absolutely nothing to do with the shell.

    Very sophisticated scripts are possible using bash and ksh, there is
    even a form of ksh that has tk capabilities! (tksh). The Python and
    Bourne-derived languages are however fundamentally different
    creatures, and use very different data models. You should **not**
    write Python (or Perl) scripts as if they were shell scripts -- doing
    so is very bad practice. When you want a shell script, write a shell
    script. When you write a Python script, write a Python script. It
    really is that simple.


    As a rule of thumb, when you have need of data structures beyond what
    scalar strings and very simple word lists can provide -- you should
    use Python. bash and ksh provide support for arrays, and ksh even has
    dictionaries! (Hashes in Perl speak.) That makes programming in bash/
    ksh more robust then pure sh, but also less portable. The best time to
    use bash is when you require bash specific features, other wise don't
    use bash. The same can be said for ksh.

    When the words array, dictionary, class, object, and/or using multiple
    source files comes to mind when implementing a program - you probably
    want to use Python, Perl, Ruby, or some other general programming
    language, not a shell scripting language like bash.

    You should be cautious to avoid mixing bash and Python code in one
    file.



    If maintainability is not a factor in what you are writing, then you
    should probably not be writing code in any language unless it is the
    language of Mathematics (and even then, maintainability is a wise
    consideration).

    --
    TerryP.
    Just Another Programmer.
     
    TerryP, Oct 14, 2009
    #3
  4. Peng Yu

    Falcolas Guest

    Re: python along or bash combined with python (for manipulatingfiles)

    On Oct 13, 10:18 pm, TerryP <> wrote:
    > On Oct 14, 2:13 am, Peng Yu <> wrote:
    >
    > > Bash is easy to use on manipulating files and directories (like change
    > > name or create links, etc) and on calling external programs. For
    > > simple functions, bash along is enough. However, bash does not support
    > > the complex functions. Python has a richer library that could provide
    > > support for complex functions (such compute the relative path between
    > > two paths).

    >
    > > I'm wondering for a task that can not be done with bash along whether
    > > it would be better to do in pure python or with a mix of both python
    > > and bash. What I care is mostly coding speed and a little bit
    > > maintainability (but not much). Can somebody provide some experience
    > > on when to combine python and bash and when to use pure python?

    >
    > bash can **not** manipulate files and directories beyond things like
    > the '>' and '>>' I/O redirections, and some minor loading/saving of
    > state data from/to files (command history, directory stack, etc). Most
    > of what you refer to are **separate operating system specific
    > programs** and have absolutely nothing to do with the shell.
    >
    > Very sophisticated scripts are possible using bash and ksh, there is
    > even a form of ksh that has tk capabilities! (tksh). The Python and
    > Bourne-derived languages are however fundamentally different
    > creatures, and use very different data models. You should **not**
    > write Python (or Perl) scripts as if they were shell scripts -- doing
    > so is very bad practice. When you want a shell script, write a shell
    > script. When you write a Python script, write a Python script. It
    > really is that simple.
    >
    > As a rule of thumb, when you have need of data structures beyond what
    > scalar strings and very simple word lists can provide -- you should
    > use Python. bash and ksh provide support for arrays, and ksh even has
    > dictionaries! (Hashes in Perl speak.) That makes programming in bash/
    > ksh more robust then pure sh, but also less portable. The best time to
    > use bash is when you require bash specific features, other wise don't
    > use bash. The same can be said for ksh.
    >
    > When the words array, dictionary, class, object, and/or using multiple
    > source files comes to mind when implementing a program - you probably
    > want to use Python, Perl, Ruby, or some other general programming
    > language, not a shell scripting language like bash.
    >
    > You should be cautious to avoid mixing bash and Python code in one
    > file.
    >
    > If maintainability is not a factor in what you are writing, then you
    > should probably not be writing code in any language unless it is the
    > language of Mathematics (and even then, maintainability is a wise
    > consideration).
    >
    > --
    >   TerryP.
    > Just Another Programmer.


    With all of Terry's admonitions in mind, Python scripts do integrate
    very well as a individual tool within a shell toolchain. With the
    multiple command line parsers and the ease of reading stdin and
    writing to stdout, it's fairly trivial to make a script which
    integrates cleanly into a bash script (or oneliner). It's trivial to
    implement a script which will either work with files, or work with
    stdin/stdout.

    Garrick
     
    Falcolas, Oct 14, 2009
    #4
  5. Peng Yu

    Aahz Guest

    Re: python along or bash combined with python (for manipulatingfiles)

    In article <>,
    TerryP <> wrote:
    >
    >Very sophisticated scripts are possible using bash and ksh, there is
    >even a form of ksh that has tk capabilities! (tksh). The Python and
    >Bourne-derived languages are however fundamentally different
    >creatures, and use very different data models. You should **not**
    >write Python (or Perl) scripts as if they were shell scripts -- doing
    >so is very bad practice. When you want a shell script, write a shell
    >script. When you write a Python script, write a Python script. It
    >really is that simple.


    Oh, well, I guess I follow bad practice a lot. Shame on me.

    (That is, I disagree that it's bad practice to use Python as if it were
    a straight scripting language, os.system() and all. I prefer using
    Python because it makes it easy to upgrade scripts as needed.)
    --
    Aahz () <*> http://www.pythoncraft.com/

    "To me vi is Zen. To use vi is to practice zen. Every command is a
    koan. Profound to the user, unintelligible to the uninitiated. You
    discover truth everytime you use it."
     
    Aahz, Oct 18, 2009
    #5
  6. Peng Yu

    Peng Yu Guest

    Re: python along or bash combined with python (for manipulatingfiles)

    On Tue, Oct 13, 2009 at 11:18 PM, TerryP <> wrote:
    > On Oct 14, 2:13 am, Peng Yu <> wrote:
    >> Bash is easy to use on manipulating files and directories (like change
    >> name or create links, etc) and on calling external programs. For
    >> simple functions, bash along is enough. However, bash does not support
    >> the complex functions. Python has a richer library that could provide
    >> support for complex functions (such compute the relative path between
    >> two paths).
    >>
    >> I'm wondering for a task that can not be done with bash along whether
    >> it would be better to do in pure python or with a mix of both python
    >> and bash. What I care is mostly coding speed and a little bit
    >> maintainability (but not much). Can somebody provide some experience
    >> on when to combine python and bash and when to use pure python?

    >
    > bash can **not** manipulate files and directories beyond things like
    > the '>' and '>>' I/O redirections, and some minor loading/saving of
    > state data from/to files (command history, directory stack, etc). Most
    > of what you refer to are **separate operating system specific
    > programs** and have absolutely nothing to do with the shell.
    >
    > Very sophisticated scripts are possible using bash and ksh, there is
    > even a form of ksh that has tk capabilities! (tksh). The Python and
    > Bourne-derived languages are however fundamentally different
    > creatures, and use very different data models. You should **not**
    > write Python (or Perl) scripts as if they were shell scripts -- doing
    > so is very bad practice. When you want a shell script, write a shell
    > script. When you write a Python script, write a Python script. It
    > really is that simple.
    >
    >
    > As a rule of thumb, when you have need of data structures beyond what
    > scalar strings and very simple word lists can provide -- you should
    > use Python. bash and ksh provide support for arrays, and ksh even has
    > dictionaries! (Hashes in Perl speak.) That makes programming in bash/
    > ksh more robust then pure sh, but also less portable. The best time to
    > use bash is when you require bash specific features, other wise don't
    > use bash. The same can be said for ksh.


    Do you know what are bash and ksh specific features? Is there a thing
    that bash/ksh can do but python can not do?

    > When the words array, dictionary, class, object, and/or using multiple
    > source files comes to mind when implementing a program - you probably
    > want to use Python, Perl, Ruby, or some other general programming
    > language, not a shell scripting language like bash.
    >
    > You should be cautious to avoid mixing bash and Python code in one
    > file.
    >
    >
    >
    > If maintainability is not a factor in what you are writing, then you
    > should probably not be writing code in any language unless it is the
    > language of Mathematics (and even then, maintainability is a wise
    > consideration).
    >
    > --
    >  TerryP.
    > Just Another Programmer.
    > --
    > http://mail.python.org/mailman/listinfo/python-list
    >
     
    Peng Yu, Oct 19, 2009
    #6
    1. Advertising

Want to reply to this thread or ask your own question?

It takes just 2 minutes to sign up (and it's free!). Just click the sign up button to choose a username and then you can ask your own questions on the forum.
Similar Threads
  1. William Park

    (patch for Bash) GTK+2 + Bash

    William Park, Jul 11, 2005, in forum: XML
    Replies:
    3
    Views:
    2,320
    William Park
    Jul 14, 2005
  2. Josh
    Replies:
    1
    Views:
    305
    Terry Reedy
    Dec 6, 2003
  3. Gary Wessle

    combined files together

    Gary Wessle, May 6, 2006, in forum: Python
    Replies:
    4
    Views:
    292
    Gary Wessle
    May 10, 2006
  4. Esmail
    Replies:
    23
    Views:
    1,090
    Esmail
    Mar 25, 2009
  5. Esmail
    Replies:
    0
    Views:
    393
    Esmail
    Mar 21, 2009
Loading...

Share This Page