Python เรียกใช้ Command line

การเรียกใช้ command line จาก Python สามารถเรียกได้โดยใช้คำสั่ง popen
import os
ipFile = “ip.txt”
opFile = “op.txt”
cmd = ‘swath <%s> %s’ % (ipFile, opFile)
f = os.popen(cmd)

หรือใช้ Popen
import subprocess
subprocess.Popen([r”gzip”,”-d”, “x.txt.gz”]).wait()

ที่มา: Subprocess managementMaking System Calls in Perl and Python

การใช้งานคำสั่ง tar บน linux

ตัวอย่างไฟล์งานชื่อ test

วิธีรวมเป็นไฟล์เดียว
tar -cvf test.tar test
ผลลัพธ์จะได้ไฟล์ test.tar

วิธี zip
gzip test.tar
ผลลัพธ์จะได้ไฟล์ test.tar.gz

วิธี unzip
tar -zxvf test.tar.gz
ผลลัพธ์จะได้ไฟล์ test

อ้างอิง expert2you.com