How to do regular BASH work in Python?

  • Thread starter Frantisek Malina
  • Start date
F

Frantisek Malina

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

Michael Torrie

Frantisek said:
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.
 

Ask a Question

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

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

Forum statistics

Threads
473,995
Messages
2,570,230
Members
46,817
Latest member
DicWeils

Latest Threads

Top