OS module +sys Module +random Module +shutil module

Source: Internet
Author: User
Tags shuffle

OS.GETCWD () Gets the current working directory, which is the directory path of the current Python script work
Os.chdir ("dirname") changes the current script working directory, equivalent to the shell CD
Os.curdir returns the current directory: ('. ')
Os.pardir Gets the parent directory string name of the current directory: (' ... ')
Os.makedirs (' dirncame1/dirname2 ') can generate multi-level recursive directories
Os.removedirs (' dirname1 ') if the directory is empty, then delete, and recursively to the previous level of the directory, if also empty, then delete, and so on
Os.mkdir (' dirname ') generates a single-level directory, equivalent to mkdir dirname in the shell
Os.rmdir (' dirname ') delete the single-level empty directory, if the directory is not empty can not be deleted, error, equivalent to the shell rmdir dirname
Os.listdir (' dirname ') lists all files and subdirectories in the specified directory, including hidden files, and prints as a list
Os.remove () Delete a file
Os.rename ("Oldname", "newname") renaming files/directories
Os.stat (' path/filename ') get File/directory information
OS.SEP output operating system-specific path delimiter, win under "\ \", Linux for "/"
OS.LINESEP output The line terminator used by the current platform, win under "\t\n", Linux "\ n"
OS.PATHSEP output is used to split the file path of the string win down for;, Linux for:
The Os.name output string indicates the current usage platform. Win-> ' NT '; Linux-> ' POSIX '
Os.system ("Bash command") runs a shell command that directly displays
Os.environ Getting system environment variables
Os.path.abspath (path) returns the absolute path normalized by path
Os.path.split (path) splits path into directory and file name two tuples returned
Os.path.dirname (path) returns the directory of path. is actually the first element of Os.path.split (path)
Os.path.basename (Path) 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, false if path does not exist
Os.path.isabs (path) returns True if path is an absolute path
Os.path.isfile (path) returns True if path is a file that exists. otherwise returns false
Os.path.isdir (path) returns True if path is a directory that exists. otherwise returns false
Os.path.join (path1[, path2[, ...]) returns a combination of multiple paths, and 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
Os.path.getsize (path) returns the size of path

# OS modules are OS-related

Import OS

# Get the current directory
# Print (OS.GETCWD ())

# Print (Os.listdir (R '/users/zhangrenguo/pycharmprojects/passers-by Conversation/day12 '))
# [' 01 closure function. py ', ' 03 decorator fix. Py ', ' 02 no parameter decorator. Py ']

# Print (Os.listdir (R '))
# Print (Os.listdir (R ' ... '))

# switch Directories
# Os.chdir (R '/users/zhangrenguo/pycharmprojects/passers-by dialogue/day11 ')
# Print (OS.GETCWD ())
# Print (Os.listdir (R '))

# Recursive creation of empty directories
# Os.mkdir (R ' a ')
# os.makedirs (R ' a/b/c/d ')

# Delete the directory if it is empty, if the upper level is also empty, also delete, etc.
# Os.removedirs (R ' a/b/c/d ')

# Delete a file
# Os.remove (R ' d ')

# os.rename (' oldname ', ' newname ')

# get file \ Directory information
# Obj=os.stat (R '/users/zhangrenguo/pycharmprojects/passers-by dialogue/day18/os module. Py ')
# print (obj)

# The path delimiter for the current file
# Print (OS.SEP)
# line Delimiter
# Print (OS.LINESEP)
# Environment Delimiter
# Print (OS.PATHSEP)

# Execute system commands
# Os.system (' tasklist ') #行不通

# Sys.path is used in module import
# Os.environ is used in
# Print (Os.environ)
# import JSON
# # When all the files in the program need to refer to a variable, you need to add the variable to the environment variable
# os.environ[' x ']=json.dump ([' A ', ' B ', ' C '])
# Print (Json.loads (os.environ[' x ')) [0])

# Os.path

# Print (Os.path.split (R '/users/zhangrenguo/pycharmprojects/passers-by Conversation/day18 '))
#
# Print (Os.path.dirname (R '/users/zhangrenguo/pycharmprojects/passers-by Conversation/day18 '))
#
# Print (Os.path.basename (R '/users/zhangrenguo/pycharmprojects/passers-by Conversation/day18 '))

Print (Os.path.join (' A ', ' B ', ' c.txt '))




#方式一: Recommended import os# specific application Import Os,syspossible_topdir = Os.path.normpath (Os.path.join (Os.path.abspath    ) ,    Os.pardir, #上一级    os.pardir,    os.pardir)) Sys.path.insert (0,possible_topdir)


——————————————————————————————————————————————————————————————————————————————————————————————————————————————

Import Random

# random can only be any decimal between 0 and 1
# Print (Random.random ())

# randomly contains 1 of all integers that do not contain 7
# Print (Random.randrange (1,7))

# 1 to 4 for all integers
# Print (Random.randint (1,4))

# Choose one More
# Print (Random.choice ([1,2,3,4]))

# Multi-choice
# Print (Random.sample ([1,2,3,4],2))

# randomly take all decimals that are greater than one less than three
# Print (Random.uniform (1,3))

# Shuffle
# item=[' A ', ' B ', ' C ', ' d '
# Random.shuffle (item)
# Print (item)


————————————————————————————————————————————————————————————————————————————————————————————————————————-——————
Import Sys

# Sys.path


# parameters used to receive the Python interpreter followed by the Py file
Print (SYS.ARGV)
Src_file = sys.argv[1]
Dest_file = sys.argv[2]

With open (Src_file, ' RB ') as Read_f,\
Open (Dest_file, ' WB ') as Write_f:
For line in Read_f:
Write_f.write (line)

——————————————————————————————————————————————————————————————————————————————————————————————————————————————

#将/data file package to place the current program directory
Import Shutil
ret = shutil.make_archive ("Data_bak", ' Gztar ', root_dir= '/data ')


/data files under the #将 to place the/tmp/directory
Import Shutil
ret = shutil.make_archive ("/tmp/data_bak", ' Gztar ', root_dir= '/data ')

Import ZipFile

# compression
z = zipfile. ZipFile (' Laxi.zip ', ' W ')
Z.write (' A.log ')
Z.write (' Data.data ')
Z.close ()

# Unzip
z = zipfile. ZipFile (' Laxi.zip ', ' R ')
Z.extractall (path= '. ')
Z.close ()

ZipFile Compression Decompression

Import Tarfile

# compression
>>> 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 ()


# Unzip
>>> t=tarfile.open ('/tmp/egon.tar ', ' R ')
>>> t.extractall ('/egon ')
>>> T.close ()

Tarfile Compression Decompression

OS module +sys Module +random Module +shutil module

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.