How to do regular BASH work in Python?

Michael Torrie torriem at
Fri Oct 10 18:32:55 CEST 2008

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

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 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.

More information about the Python-list mailing list