views:

576

answers:

2

I need to mount a directory "dir" on a network machine "data" using python on a linux machine

I know that I can send the command via command line:

mkdir ~/mnt/data_dir mount -t data:/dir/ /mnt/data_dir

but how would I send that command from a python script?

+2  A: 

Here is one way:

import os

os.cmd ("mkdir ~/mnt/data_dir mount -t data:/dir/ /mnt/data_dir")

You can also use "popen" if you want to read the output of the command in your script.

HIH

...richie

Richie
A better example would use `subprocess.Popen`.
S.Lott
+2  A: 

Example using the subprocess module:

import subprocess

subprocess.Popen(["mkdir", "~/mnt/data_dir", "mount", "-t", "data:/dir/", "/mnt/data_dir"])

OR

import subprocess

subprocess.Popen("mkdir ~/mnt/data_dir mount -t data:/dir/ /mnt/data_dir", shell=True)

The second version uses the shell to execute the command. While more readable and easier to use in most situations, it should be avoided when passing user submitted arguments as those might lead to shell injection (i.e. execution of other commands than mkdir in this case).

sttwister