Day08 OS与sys模块以及shutil模块
1.os模块
import os
path='D:\\'
os.getcwd()
os.chdir("dirname")
os.curdir
os.pardir
os.makedirs('dirname1/dirname2')
os.removedirs('dirname1')
os.mkdir('dirname')
os.rmdir('dirname')
os.listdir('dirname')
os.remove()
os.rename("oldname","newname")
os.stat('path/filename')
os.sep
os.linesep
os.pathsep
os.name
os.system("bash command")
os.environ
os.path.abspath(path)
os.path.split(path)
os.path.dirname(path)
os.path.basename(path)
os.path.exists(path)
os.path.isabs(path)
os.path.isfile(path)
os.path.isdir(path)
os.path.join(path,[path,[...]])
os.path.getatime(path)
os.path.getmtime(path)
os.path.getsize(path)
2.sys模块
sys.argv
sys.exit(n)
sys.version
sys.maxint
sys.path
sys.platform
3.shutil模块(了解)
import shutil
shutil.copyfileobj(fsrc, fdst[, length])
shutil.copyfile(src, dst)
shutil.copymode(src, dst)
shutil.copystat(src, dst)
shutil.copy(src, dst)
shutil.copy2(src, dst)
shutil.copytree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
shutil.rmtree(path[, ignore_errors[, onerror]])
shutil.move(src, dst)
shutil.make_archive(base_name, format,root_dir...)
import shutil
ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data')
import shutil
ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data')
import zipfile
z = zipfile.ZipFile('laxi.zip', 'w')
z.write('a.log')
z.write('data.data')
z.close()
z = zipfile.ZipFile('laxi.zip', 'r')
z.extractall(path='.')
z.close()
zipfile压缩解压缩
import tarfile
>>> t=tarfile.open('/tmp/egon.tar','w')
>>> t.add('/test1/a.py',arcname='a.bak')
>>> t.add('/test1/b.py',arcname='b.bak')
>>> t.close()
>>> t=tarfile.open('/tmp/egon.tar','r')
>>> t.extractall('/egon')
>>> t.close()
tarfile压缩解压缩