Python3-sys&shutil module Real-combat analysis

Source: Internet
Author: User

1.sys Module

Import SYS Print (sys.version)  # Get Python parser version information Print # get the program itself path Print (Sys.path)  # returns the search path for the module, initializing the value using the PYTHONPATH environment variable Print # returns the name of the operating platform

2.shutil advanced files, folders, compression package processing module

Importshutilshutil.copyfileobj (Open ('a.txt.py','R'), open ('a.py1','W'))#Copy the contents of a file to another fileShutil.copyfile ('a.py1','a2.py')#Copy FilesShutil.copymode ('a2.py','a.txt.py')#Copy permissions only. Original file content, group, user is unchangedShutil.copystat ('a3.py','a4.py')#information on the status of the copy, including: mode bits, Atime, mtime, FlagsShutil.copy ('a3.py','a4.py')#copying files and permissionsShutil.copy2 ('a3.py','a4.py')#Copying files and status informationShutil.copytree ('A1','A2')#shutil.ignore_patterns (*patterns) recursive copy file (copy directory)Shutil.rmtree ('A2','A1')#recursively delete a directoryShutil.move ('d:/Software/pycharm/day5/a.py1','d:/Software/pycharm')#to move a file recursively#shutil.make_archive (Compression save path, compressed package type, compressed target path), compressed package type, "Zip", "tar", "Bztar", "Gztar"Shutil.make_archive ('d:/Software/shutil.make_ar','Zip','d:/Software/pycharm/day5')#def _make_tarball (Base_name, Base_dir, compress= "gzip", Verbose=0, Dry_run=0,owner=none, Group=none, Logger=None ): Default is tar.gz package compression directoryShutil._make_tarball ('d:/Software/shutil.make_ar','d:/Software/pycharm/day5')#shutil._make_zipfile (Base_name, Base_dir, verbose=0, Dry_run=0, logger=none): Default Zip package compression directoryShutil._make_zipfile ('d:/Software/shutil.make_ar','d:/Software/pycharm/day5')#shutil processing of compressed packets is done by calling the ZipFile and Tarfile two modules, in detail:ImportZipFile#Zip Package Decompressionz = zipfile. ZipFile ('d:/Software/shutil.make_ar.zip')#Specify unpacking PackageZ.extractall ()#unzip to the specified path, the default is to extract the current path to the programz.close ()#Compress Zip Packagez = zipfile. ZipFile ('d:/Software/shutil_shiyan.zip','W')#Open and specify the package save path (must be a zip suffix)Z.write ('D:/a.txt')#add a file or directory to a compressed packageZ.write ('C:/shutil.make_ar.zip')#add a file or directory to a compressed packageZ.write ('c:/b')#add a file or directory to a compressed packagez.close ()#tar.gz Package DecompressionImportTarfilegz= Tarfile.open ('d:/Software/shutil.make_ar.tar.gz')#Specify unpacking PackageGz.extractall ('d:/Software/py_shiyan')#Specify save pathgz.close ()#tar.gz Package Compressiontar = Tarfile.open ('d:/Software/shiyan.tar.gz','W')#Specify the save path for the compressed packageTar.add ('D:/a.txt')#add a file or directory to a compressed packageTar.add ('C:/shutil.make_ar.zip')#add a file or directory to a compressed packageTar.add ('c:/b')#add a file or directory to a compressed packageTar.close ()

Python3-sys&shutil module Real-combat analysis

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.