Does anyone know what I am doing wrong with this command:
import subprocess
subprocess.call('tar -zvxf %s -C %s' % ("file.tar.gz", '/folder'), shell=True)
The code runs without any errors but the file is only unzipped on random occasions. I know I can use tarfile
, but I would like to run it as a Linux command. Any ideas?
If you read the man page, you'll see that the -C
parameter is sensitive to order -- it only affects operations that come after it on the command line. So, your file is being unzipped into whatever random directory you happen to be in.
You don't need shell for this. Do:
import os
import subprocess
os.chdir('/folder')
subprocess.call( ['tar','xvfz', 'file.tar.gz'] )
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.