我寫這個劇本焦油一些備份:Python和焦油命令shell =真
date = str(now.year)+str(now.month)+str(now.day)
tar="tar -pczf "+date+"backup_lucas.tar.gz /home/lucas/backup/"
subprocess.Popen(tar)
但後來我得到:
File "test.py", line 21, in <module>
subprocess.Popen(tar)
File "/usr/lib/python2.6/subprocess.py", line 623, in __init__
errread, errwrite)
File "/usr/lib/python2.6/subprocess.py", line 1141, in _execute_child
raise child_exception
OSError: [Errno 2] No such file or directory
當我添加添加殼=真到了POPEN命令它作品:
subprocess.Popen(tar,shell=True)
但是我聽說shell = True是要避免的,因爲它有時不安全(?)。
如何在不使用shell = True的情況下成功發出命令?