I have to run an external program from python. External program makes many temporary files (which conflict with other instance of same program). My strategy is
Get current directory (native) path, change to temp directory, run external command, copy output to native directory, change back to native directory.
Here is my code
import os, tempfile
current_dir = os.getcwd()
tmp_dir = tempfile.mkdtemp()
input_file, outfile = 'input_file', 'result'
os.system('cp %s %s' %(input_file, tmp_dir))
os.chdir(tmp_dir)
cmd = 'pwd> tmp_01.txt;ls -ltr >>tmp_01.txt' ##system command goes here
os.system(cmd)
os.system('cp tmp_01.txt %s/%s' %(current_dir, outfile))
os.chdir(current_dir)
My questions are:
Is it right or there are simple way to do it?
Do I need to remove the tmp directory?
Is it safe to use this code as python module/class in a web application? Does it makes shell injection essay or can possible reveal more information then needed?