python base (19): random module, time module, sys module, os module

1. random module

Introducing the random module, the format is:

import random

1.1 random decimal

Take a random decimal: mathematical calculations.

Print (random.random ()) # decimal taken between 0-1 Print (random.uniform (1,2)) takes a decimal between 1-2 #

1.2 random integer

Take random integers: lottery, lottery has used.

print (random.randint (1,2)) # [1,2] print (random.randrange (1,2)) # 1.2) print (random.randrange (1,200,2)) # 1.2 )

1.3 randomly selected value from the list

Value randomly selected from a list, such as sweepstakes.

l = ['a','b',(1,2),123]
print(random.choice(l))print(random.sample(l,2))

1.4 disrupt the order of the list

Disrupt a list order in the list on the basis of the original modified directly, saving space, e.g. shuffling.

random.shuffle(l)
print(l)

2. time module

time and time module is mainly used to dealing

Time is introduced into the module, the format is:

import time

Time format:

'2018-8-20' 2018.8.20 'string data type formatted time - posters 
structured time 
1,534,732,642.617272 floating-point data type, the time stamp in s - to the machine calculation of 
197,011 0: 0: 0

2.1 Time timestamp

print(time.time())

2.2 Formatting time

print(time.strftime('%Y-%m-%d %H:%M:%S')) # str format timeprint(time.strftime('%y-%m-%d %H:%M:%S')) # str format timeprint(time.strftime('%c'))

2.3 Structure of the time

struct_time = time.localtime()  # 北京时间print(struct_time)print(struct_time.tm_mon)

2.4 time stamp to a string

print(time.time())
struct_time = time.localtime(1500000000)# print(time.gmtime(1500000000))ret = time.strftime('%y-%m-%d %H:%M:%S',struct_time)print(ret)

String transfer time stamp 2.5

struct_time = time.strptime('2018-8-8','%Y-%m-%d')
print(struct_time)res = time.mktime(struct_time)print(res)

3. sys module

sys is a Python interpreter and dealing

Sys module is introduced, the format is:

import sys

3.1 argv

Print (the sys.argv)   # the argv first parameter is the command value of this latter python usr = INPUT ( 'username') pwd = INPUT ( 'password') usr = the sys.argv [. 1] pwd = the sys.argv [ 2] IF usr == 'xhh' and pwd == 'xhh0308':   Print ( 'Login successful') the else:   Exit ()

3.2 path

Module is the presence of the interpreter is it? Not.
Module should exist on the hard disk,
but when I use the import -> This module into memory only.

A module can be successfully introduced, it all depends on sys.path Here there is no such module is located.
Custom module when importing modules when, still need to focus on sys.path.

3.3 modules

print (sys.modules) # We are the names of all modules into memory: the memory address of the module print (. sys.modules [ 're' ] findall ( '\ d', 'abc126'))

4. os module

Os module is introduced, the format is:

import os

os is the operating system and interactive modules

4.1 os module common method

os.makdirs ( ' dirname1 / dirname2 ' ) may generate a multilayer recursive directory 
os.removedirs ( ' dirname1 ' ) if the directory is empty, delete, and recursively to the parent directory, should be empty, delete, and so analogy 
os.mkdir ( ' dirname ' ) to generate a single level directory; is equivalent to the shell dirname mkdir 
os.rmdir ( ' dirname ' ) empty directory delete a single stage, if the directory is not empty can not be deleted, being given; corresponds to the shell rmdir dirname 
os.listdir ( ' dirname ' ) lists all the files and subdirectories in the specified directory, including hidden files, and print in a list 
os.remove () to delete a file 
os.rename ( " oldname " , " newname " ) weight name the file / directory 
os.State ( 'path / filename ' ) get the file / directory information 

os.system ( " bash the Command " ) to run shell commands, displayed directly 
os.popen ( " bash the Command) .read () run shell commands to get the results 
os.getcwd () Gets the current working directory that the current directory path python script work 
os.chdir ( " dirname " ) script to change the current working directory; under the equivalent cd shell 


os.path 
os.path.abspath (path) returns the absolute path to the path of standardized 
os. path.split (path) into the path directory and file name tuple returned 
os.path.dirname (path) returns the directory path of the fact that the first element os.path.split (path) of 
os.path. basename (path) returns the last path of the file name. how path to / \ or end, it will return a null value. That os.path.split (path) of the second element 
os.path.exists (path) if the path exist, return True; if the path does not exist, returns False 
os.path.isabs (path) if the path is the absolute path, returns True
os.path.isfile (path) if the path is a file exists, returns True. Otherwise it returns False 
os.path.isdir (path) if the path is a directory exists, then return True. Otherwise it returns False 
os.path.join (path1 [, path2 [, ...]]) will return after a combination of multiple paths parameters before the first absolute path will be ignored 
os.path.getatime (path) return path points to the last access time of a file or directory 
os.path.getmtime (path) returns the file or directory path points to the last modification time 
os.path.getsize (path) return path size

4.2 stat structure

Note: the os.stat ( 'path / filename' ) obtaining the file / directory information  of the structure description  

stat structure: 

The st_mode: the inode protection mode 
st_ino: inode node number. 
st_dev: inode device resides. 
st_nlink: inode number of links. 
st_uid: User ID of the owner. 
st_gid: group ID of the owner. 
st_size: common file unit size in bytes; wait for data containing special file. 
st_atime: Time last accessed. 
st_mtime: last modification time. 
st_ctime: report by the operating system's " ctime " . On some systems (such as Unix) is the time of most recent metadata changes, on the other systems (such as Windows) is the creation time (see platform documentation for more information).

Properties 4.3 os module

os.sep output operation system-specific path separator for the next win " \\ " , is the Linux " / " 
os.linesep output current platform using line terminator, to win the " \ R & lt \ n- " , under Linux as " \ n- " 
is the character string for dividing a win os.pathsep output file path;, under Linux is: 
the os.name output string indicating the current use internet. win -> ' NT ' ; Linux-> ' POSIX '

Guess you like

Origin www.cnblogs.com/liuhui0308/p/11823243.html