Day6 SYS module and day6sys Module

Source: Internet
Author: User

Day6 SYS module and day6sys Module

SYS module

Provides operations related to the Python Interpreter:

(1)Sys. argv command line parametersListThe first element is the program path.

>>> Sys. argv
['']

(2)Sys. exit (n) exit the program. exit (0)

(3)Sys. version: get the version information of the Python interpreter.

(4)The largest value of sys. maxintIntValue

(5)Sys. path: return the search path of the module. When initializing, useValue

>>> Sys. path
['', '/Usr/local/lib/python3.5/dist-packages/pygame-1.9.4.dev0-py3.5-linux-x86_64.egg', '/usr/lib/python35.zip', '/usr/lib/python3.5 ', '/usr/lib/python3.5/load', '/usr/lib/python3.5/lib-dynload', '/home/zhuzhu /. local/lib/python3.5/site-packages ','/usr/local/lib/python3.5/dist-packages ','/usr/lib/python3/dist-packages ']
(6)Sys. platform return to the operating system platformName

>>> Sys. platform
'Linux'
(7)Sys. stdin Input

(8)Sys. stdout output

(9)Sys. stderror Error

Progress percentage:

import time,sysdef view_bar(num,total):    rate = float(num) / float(total)    rate_num = int(rate * 100)    r = '\r%d%%' %(rate_num,)    sys.stdout.write(r)    sys.stdout.flush()if __name__ == "__main__":    for i in range(0,100):        time.sleep(0.1)        view_bar(i,100)

Shutil Module

Advanced file, folder, and compressed Package Processing Module

(1) copyfileobj (fsrc, fdst, length = 16*1024)

Def copyfileobj (fsrc, fdst, length = 16*1024 ):
"Copy data from file-like object fsrc to file-like object fdst """
While 1:
Buf = fsrc. read (length)
If not buf:
Break
Fdst. write (buf)

Shutil. copyfileobj (fsrc, fdst [, length])

Import shutil # import module

Shutil. copyfileobj (open ("old_file", "r"), open ("new_file", "w") # open two files and copy the content of one file to another.

(2)Shutil. copyfile (src, dst)

Def copyfile (src, dst, *, follow_symlinks = True ):
"Copy data from src to dst.

If follow_symlinks is not set and src is a symbolic link, a new
Symlink will be created instead of copying the file it points.

"""

Import shutil # import a file

Shutil. copyfile ("old_file", "new_file") # import file information to another file

(3)Shutil. copymode (src, dst)

Only copy permission. Contents, groups, and users remain unchanged

shutil.copymode('f1.log', 'f2.log')

(4)Shutil. copystat (src, dst)

Only copy status information, including: mode bits, atime, mtime, flags

shutil.copystat('f1.log', 'f2.log')

(5)Shutil. copy (src, dst)

Copy files and permissions

shutil.copy('f1.log', 'f2.log')

(6)Shutil. copy2 (src, dst)

Copy file and status information

shutil.copy2('f1.log', 'f2.log')

(7)Shutil. ignore_patterns (* patterns)

(8)Shutil. copytree (src, dst, symlinks = False, ignore = None)

Recursively copy folders

import shutiL

shutil.copytree('folder1', 'folder2', ignore=shutil.ignore_patterns('*.pyc', 'tmp*'))

(9)Shutil. rmtree (path [, ignore_errors [, onerror])

Recursively delete an object

import shutiL

shutil.rmtree('folder1')

(10)Shutil. move (src, dst)

Recursively move a file, which is similar to the mv command, is actually renaming

import shutiL

shutil.move('folder1', 'folder3')

(11)Shutil. make_archive (base_name, format ,...)

Create a compressed package and return the file path, such as zip and tar.

Create a compressed package and return the file path, such as zip and tar.

Base_name: the file name of the compressed package. It can also be the path of the compressed package. Only when the file name is used, it is saved to the current directory; otherwise, it is saved to the specified path.

For example, save www => to the current path

For example:/Users/wupeiqi/www => Save to/Users/wupeiqi/

Format: Compressed package type, "zip", "tar", "bztar", and "gztar"

Root_dir: Path of the folder to be compressed (default current directory)

Owner: user. The current user is used by default.

Group: group. Default Value: current group.

Logger: used to record logs, usually a logging. Logger object.

# Package files in/Users/wupeiqi/Downloads/test to the current program directory

import shutiL

ret = shutil.make_archive("wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')

# Package/Users/wupeiqi/Downloads/test files and place them in the/Users/wupeiqi/directory.

import shutiL

ret = shutil.make_archive("/Users/wupeiqi/wwwwwwwwww", 'gztar', root_dir='/Users/wupeiqi/Downloads/test')

Shutil calls ZipFile and TarFile to process the compressed package. Details:

Import zipfile # compress z = zipfile. zipFile ("lowb.zip", "w") # first open the file and add the file to be compressed to the file. write ("new_file") z. write ("old_file") # Add a file to the file, decompress it together, and add the compressed file z. close () # decompress z = zipfile. zipFile ("lowb.zip", "r") # first open the file and decompress it. extractall () # decompress the file z. close ()

Tar format compression and decompression

Import tarfile # compress the file # tar = tarfile. open ("lowB.tar", "w") # first open the file and add the file to be compressed # tar. add ("new_file") # tar. add ("old_file") # tar. close () # decompress the file tar = tarfile. open ("lowB.tar", "r") # open the file and decompress tar. extractall () # You can set the extract address tar. close ()

To decompress and decompress a file, you must first open the file, add the file to it, and add the file to be compressed. to decompress the file, you must first open the file and decompress it using extractall.

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.