Python 3.5 (15) built-in modules

Source: Internet
Author: User
Tags sha1 python script

Built-in Modules

First, OS

Used to provide system-level operations

OS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work os.chdir ("dirname") To change the current script working directory, equivalent to the shell under Cdos.curdir return to the current directory: ('.') Os.pardir Gets the parent directory string name of the current directory: ('..') Os.makedirs ('dirname1/dirname2') to generate a multi-level recursive directory Os.removedirs ('dirname1'If the directory is empty, it is deleted and recursively to the previous level of the directory, if it is also empty, then delete, and so on Os.mkdir ('dirname') to generate a single-level directory, equivalent to the shell mkdir Dirnameos.rmdir ('dirname'Delete the single-level empty directory, if the directory is not empty can not be deleted, error, equivalent to the shell rmdir Dirnameos.listdir ('dirname'lists all files and subdirectories under the specified directory, including hidden files, and prints a list of os.remove () deletes a file Os.rename ("oldname","newname") Rename File/Catalog Os.stat ('Path/filename') Get File/directory information OS.SEP output operating system-specific path delimiter, win under"\\", under Linux for"/"os.linesep Output The line terminator used by the current platform, win under"\t\n", under Linux for"\ n"the OS.PATHSEP output string that is used to split the file path Os.name the output strings indicates the current usage platform. Win-'NT'; Linux->'POSIX'Os.system ("Bash Command"run the shell command to directly display the Os.environ get system environment variable Os.path.abspath (PATH) return path normalized absolute path Os.path.split (path) Split path into directory and file name two tuples return Os.path.dirname (path) to the directory where path is returned. In fact, the first element of Os.path.split (path) os.path.basename returns the last file name of path. If path ends with a/or \, then a null value is returned. The second element of Os.path.split (path), os.path.exists (path), returns True if path exists, or if path does not exist, returns Falseos.path.isabs if path is an absolute path, Returns Trueos.path.isfile (path) If path is an existing file and returns True. Otherwise, return Falseos.path.isdir (path) True if path is a directory that exists.  Otherwise return Falseos.path.join (path1[, path2[, ...])  When multiple paths are combined, the parameters before the first absolute path are ignored Os.path.getatime (path) returns the last access time of the file or directory to which path is pointing os.path.getmtime (path) Returns the last modified time of the file or directory to which path is pointing

Second, SYS

SYS.ARGV           command line argument list, the first element is the program itself path Sys.exit (n)        exits the program, exit normally (0) sys.version        Gets the version information of the Python interpreter Sys.maxint         the largest int value Sys.path           returns the search path for the module, using the value of the PYTHONPATH environment variable when initializing Sys.platform       Returns the operating system platform name Sys.stdout.write (' please: ') val = Sys.stdin.readline () [:-1]

Third, Hashlib

Import= md5.new () hash.update ('admin')print hash.hexdigest () Import  = sha.new () hash.update ('admin')Print Hash.hexdigest ()
ImportHashlib######### MD5 ########Hash=hashlib.md5 () hash.update ('Admin')Printhash.hexdigest ()######### SHA1 ########Hash=hashlib.sha1 () hash.update ('Admin')Printhash.hexdigest ()######### sha256 ########Hash=hashlib.sha256 () hash.update ('Admin')Printhash.hexdigest ()######### sha384 ########Hash=hashlib.sha384 () hash.update ('Admin')Printhash.hexdigest ()######### sha512 ########Hash=hashlib.sha512 () hash.update ('Admin')PrintHash.hexdigest ()

Although the above encryption algorithm is still very strong, but the time has the flaw, namely: through the collision library can reverse the solution. Therefore, it is necessary to add a custom key to the encryption algorithm to do encryption.

Import#  ######## MD5 ########= hashlib.md5 ('898oafs09f ' ) hash.update ('admin')print hash.hexdigest ()

Python comes with key encryption module

Hmca

Import= hmac.new ('wueiqi') h.update ('Hellowo ' )print h.hexdigest ()

Iv. JSON and Pickle

Two modules for serialization

    • JSON, used to convert between string and Python data types
    • Pickle for conversion between Python-specific types and Python data types

The JSON module provides four functions: dumps, dump, loads, load

The Pickle module provides four functions: dumps, dump, loads, load

The basic features are explained first:

Dumps is converting dict to str format, loads is converting str to dict format.

Dump and load are similar functions, just combined with file operations

Liu, Shutil

Advanced files, folders, Compression pack processing modules

Shutil.copyfileobj (FSRC, fdst[, length])
Copy the contents of the file to another file, which can be part of the content

Shutil.copyfile (SRC, DST)
Copy files

Shutil.copymode (SRC, DST)
Copy permissions only. Content, group, user are not changed

Shutil.copystat (SRC, DST)
Information on the status of the copy, including: mode bits, Atime, mtime, Flags

Shutil.copy (SRC, DST)
Copying files and Permissions

Shutil.copy2 (SRC, DST)
Copying files and status information

Shutil.ignore_patterns (*patterns)
Shutil.copytree (SRC, DST, Symlinks=false, Ignore=none)
Recursive de-Copying files

Example: Copytree (source, Destination, ignore=ignore_patterns (' *.pyc ', ' tmp* '))

Shutil.rmtree (path[, ignore_errors[, OnError])
To delete a file recursively

Shutil.move (SRC, DST)
To move a file recursively

Shutil.make_archive (base_name, Format,...)

Create a compressed package and return the file path, for example: Zip, tar

base_name: The file name of the compressed package, or the path to the compressed package. When the file name is only, save to the current directory, otherwise save to the specified path, such as: www=save to current path such as:/users/wupeiqi/www = Save to/users/wupeiqi/Format: Compressed package type, "Zip", "tar", "Bztar", "Gztar" Root_dir: Folder path to compress (default current directory) Owner: User, default Current user group: groups, default current group logger : Used to log logs, usually logging. Logger Object#package files under/users/wupeiqi/downloads/test to place the current program directory ImportShutilret= Shutil.make_archive ("wwwwwwwwww",'Gztar', root_dir='/users/wupeiqi/downloads/test')  #package The files under/users/wupeiqi/downloads/test to place the/users/wupeiqi/directoryImportShutilret= Shutil.make_archive ("/users/wupeiqi/wwwwwwwwww",'Gztar', root_dir='/users/wupeiqi/downloads/test')

Python 3.5 (15) built-in modules

Related Article

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.