I. shutil module (understand): advanced file, folder, zip package processing module.
import shutil # (fsrc, fdst[, length]) to copy the contents of a file to another file (open('', 'r'), open('', 'w')) # (src, dst), copy file ('', '') # Target file does not need to exist # (src, dst), copy permissions only. Content, group, and user are unchanged ('', '') # Target file must exist # (src, dst), copy only the state information, including: mode bits, atime, mtime, flags ('', '') # Target file must exist # (src, dst), copy file and permissions ('', '') # shutil.copy2(src, dst), copy file and state information shutil.copy2('', '') # shutil.ignore_patterns(*patterns) # (src, dst, symlinks=False, ignore=None), recursively copying folders # The target directory must not exist, note that the parent directory of folder2 must have writable permissions, ignore means exclude. ('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*')) # (path[, ignore_errors[, onerror]]), recursively deletes the file ('folder1') # (src, dst), to move files recursively, it is similar to the mv command, which is actually a rename. ('folder1', 'folder3') # shutil.make_archive(base_name, format, ...) to create a compressed archive and return the path to the file, e.g., zip, tar ''' base_name: the filename of the archive, can also be the path of the archive. If it is just a file name, then save it to the current directory, otherwise save it to the specified path, e.g. data_bak = > save it to the current path; / tmp/data_bak = > save it to /tmp/ format: the type of package, "zip", "tar", "bztar", "gztar " root_dir: path of the folder to be compressed (default current directory) owner: user, default is current user group: group, default current group logger: for logging, usually an object ''' # Pack the files under /data and place them in the current program directory. ret = shutil.make_archive("data_bak", 'gztar', root_dir='/data') # Pack the files under /data and place them in the /tmp/ directory. ret = shutil.make_archive("/tmp/data_bak", 'gztar', root_dir='/data')
shutil's processing of compressed packages is done by calling the ZipFile and TarFile modules, as detailed below:
1, zipfile compression decompression
import zipfile # Compression z = ('', 'w') ('') ('') () # Unzip z = ('', 'r') (path='.') ()
2, tarfile compression and decompression
import tarfile # Compression t=('/tmp/','w') ('/test1/',arcname='') ('/test1/',arcname='') () # Unzip t=('/tmp/','r') ('/egon') ()
Second, subprocess module (understand): run subroutines
The subprocess module allows you to create a new process that executes another program and communicates with it to get standard input, standard output, standard errors, and return codes.
Check out the official website for more:/2/library/?highlight=subprocess#frequently-used-arguments
import subprocess ''' sh-3.2# ls /Users/nick/Desktop |grep txt$ Things.txt ''' res1 = ('ls /Users/jieli/Desktop',shell=True, stdout=) res = ('grep txt$', shell=True,stdin=, stdout=) print(().decode('utf-8')) # The following paragraph is equivalent to the above, but it has the advantage that one data stream can interact with the other, and the results can be crawled and handed to grep. res1 = ('ls /Users/jieli/Desktop |grep txt$',shell=True, stdout=) print(().decode('utf-8')) # under windows. # dir | findstr 'test*' # dir | findstr 'txt$' res1 = (r'dir "C:\Users\Administrator\PycharmProjects\test\ function preparation"', shell=True, stdout=) res = ('findstr test*', shell=True, stdin=, stdout=) # subprocess use the current system default encoding, get the result as bytes, in windows need to use gbk decoding print(().decode('gbk') )
This is the end of this article about Python using shutil to manipulate files and subprocess to run subroutines. I hope it will be helpful to your learning, and I hope you will support me more.