Senior files, folders, archive processing module
shutil.copyfileobj (fsrc, fdst [, length
]) will copy the contents of the file to another file
import shutil shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))
shutil.copyfile (src, dst)
copies of documents
shutil.copyfile ( 'f1.log', 'f2.log') # destination file need not exist
shutil.copymode (src, dst)
only copy rights. Content, groups, users remain unchanged
shutil.copymode ( 'f1.log', 'f2.log') # target file must exist
shutil.copystat (src, dst)
only copy status information, comprising: mode bits, atime, mtime, flags
shutil.copystat ( 'f1.log', 'f2.log') # target file must exist
shutil.copy (src, dst)
to copy files and permissions
shutil import shutil.copy ( 'f1.log', 'f2.log')
shutil.copy2 (src, dst)
copy files and status information
shutil import shutil.copy2 ( 'f1.log', 'f2.log')
shutil.ignore_patterns (* Patterns)
shutil.copytree (the src, DST, symlinks = False, the ignore = None)
recursively copied to the folder
shutil Import shutil.copytree ( 'folder1', 'folder2', the ignore = shutil.ignore_patterns ( '*. pyc', 'tmp *')) # can not exist in the destination directory, attention to folder2 directory parent directory must be writable , ignore mean exclusion
import shutil
shutil.copytree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
shutil.rmtree (path [, ignore_errors [, onerror]])
recursively to delete files
shutil import shutil.rmtree ( 'folder1')
shutil.move (src, dst)
recursively to move a file, which is similar mv command, in fact, renamed.
shutil import shutil.move ( 'folder1', 'folder3')
shutil.make_archive(base_name, format,...)
Create a compressed package and return the file path, such as: zip, tar
Create a compressed package and return the file path, such as: zip, tar
base_name: archive file name, it can be compressed path. Just when the file name is saved to the current directory, or saved to a specified location,
such as data_bak => Save to the current path
, such as: / tmp / data_bak => Save to / tmp /
format: compressed packet type, "zip", "tar "," bztar "," gztar "
ROOT_DIR: to compress folder path (current default directory)
owner: the user, the default current user
group: group, default current set
logger: for logging, the object is usually logging.Logger
# The file in / data packaging program placed the current directory Import the shutil RET = shutil.make_archive ( " data_bak " , ' gztar ' , ROOT_DIR = ' / data ' ) # file in / data package placement / tmp / directory Import the shutil RET = shutil.make_archive ( " / tmp / data_bak " , ' gztar ' , ROOT_DIR = ' / Data ' )
zipfile compression and decompression
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()
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 ( '/ the') >>> t.close()
Senior files, folders, archive processing module
shutil.copyfileobj (fsrc, fdst [, length
]) will copy the contents of the file to another file
import shutil shutil.copyfileobj(open('old.xml','r'), open('new.xml', 'w'))
shutil.copyfile (src, dst)
copies of documents
shutil.copyfile ( 'f1.log', 'f2.log') # destination file need not exist
shutil.copymode (src, dst)
only copy rights. Content, groups, users remain unchanged
shutil.copymode ( 'f1.log', 'f2.log') # target file must exist
shutil.copystat (src, dst)
only copy status information, comprising: mode bits, atime, mtime, flags
shutil.copystat ( 'f1.log', 'f2.log') # target file must exist
shutil.copy (src, dst)
to copy files and permissions
shutil import shutil.copy ( 'f1.log', 'f2.log')
shutil.copy2 (src, dst)
copy files and status information
shutil import shutil.copy2 ( 'f1.log', 'f2.log')
shutil.ignore_patterns (* Patterns)
shutil.copytree (the src, DST, symlinks = False, the ignore = None)
recursively copied to the folder
shutil Import shutil.copytree ( 'folder1', 'folder2', the ignore = shutil.ignore_patterns ( '*. pyc', 'tmp *')) # can not exist in the destination directory, attention to folder2 directory parent directory must be writable , ignore mean exclusion
import shutil
shutil.copytree('f1', 'f2', symlinks=True, ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))
shutil.rmtree (path [, ignore_errors [, onerror]])
recursively to delete files
shutil import shutil.rmtree ( 'folder1')
shutil.move (src, dst)
recursively to move a file, which is similar mv command, in fact, renamed.
shutil import shutil.move ( 'folder1', 'folder3')
shutil.make_archive(base_name, format,...)
Create a compressed package and return the file path, such as: zip, tar
Create a compressed package and return the file path, such as: zip, tar
base_name: archive file name, it can be compressed path. Just when the file name is saved to the current directory, or saved to a specified location,
such as data_bak => Save to the current path
, such as: / tmp / data_bak => Save to / tmp /
format: compressed packet type, "zip", "tar "," bztar "," gztar "
ROOT_DIR: to compress folder path (current default directory)
owner: the user, the default current user
group: group, default current set
logger: for logging, the object is usually logging.Logger
# The file in / data packaging program placed the current directory Import the shutil RET = shutil.make_archive ( " data_bak " , ' gztar ' , ROOT_DIR = ' / data ' ) # file in / data package placement / tmp / directory Import the shutil RET = shutil.make_archive ( " / tmp / data_bak " , ' gztar ' , ROOT_DIR = ' / Data ' )
zipfile compression and decompression
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()
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 ( '/ the') >>> t.close()