执行shell命令
1.supprocess
2.commands
# -*- coding:utf-8 -*-import subprocessimport commands# py3/py2result = subprocess.check_output('dir',cwd='D:\m1\day07')print(result)# py2result = commands.getoutput('dir')print(result)
压缩和解压文件
import shutil #压缩用shutil # py2/py3# shutil.make_archive(r'D:\m1\day07\x1',format='zip',root_dir='D:\m1\day07\m1')# py3# shutil._unpack #解压用zipfile或tarfile# py2/py3import zipfileimport tarfileobj = zipfile.ZipFile(r'D:\m1\day07\x1.zip',mode='r')obj.extractall(r'D:\m1\day07\files')obj.close()
查看目录下所有文件
import os#os.listdir只能显示一层目录# for li in os.listdir(r'D:\m1\day07\files'):# print(li)#os.walk能显示所有的文件.# for base,folder,files in os.walk(r'D:\m1\day07\files'):# for item in files:# file_path = os.path.join(base,item)# print(file_path)
根据pid杀死进程
# -*- coding:utf-8 -*-import osimport signalos.kill(1223,signal.SIGBREAK)#signal.SIGBREAK可以用9这个数字替换