我编写了这个脚本来压缩一些备份文件:
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命令中添加shell=True时,它可以正常工作:
subprocess.Popen(tar,shell=True)
但是我听说shell=True有时不安全,应该避免使用。
没有使用shell=True,如何成功执行命令?