Chapter VI module
6.1 module
-
Built-in module, it has provided the function of internal python
import sys
-
Third-party modules, download / install / use
# Pip.exe directory where it is added to the environment variable
PIP install the module name of the installation of # example: pip install xlrd
-
Custom Module (write your own functions)
-
xxx.py
def f1():
print(123)
def f2():
print(125)-
x1.py
# Content-defined function called from inside the
Import xxx
xxx. F1 ()
xxx. F2 () -
6.2 sys
python interpreter related data
-
sys.getrefcount, obtaining a worthy application count
-
sys.getrecursionlimit, python supported by default the number of recursive
-
sys.stdout.write-->print
Print progress bar #
Import Time
for I in Range ( . 1, 101): MSG = "% S %%" % I Print ( MSG, End = "") . Time SLEEP ( 0.05)
-
sys.argv
# Allow users to execute scripts passed to delete the file path, internally to help users delete the directory
Import SYS
C: \ Python36 \ python36. EXE D: / code / s21day14 / 7. . Module parameter passing Py D: / the Test
SYS. = the argv [ D: / code / s21day14 / 7. The module transfer parameters. Py, D: / Test]
path = SYS. the argv [ . 1] Impor the shutil the shutil. rmtree ( path)
-
sys.path, default pythn to import the module, it will follow one by one sys.path looking for the Road King
Import SYS
. SYS . path the append ( 'D: \\')
Import XXX # referenced modules other than the default path
-
json
json is a special string [like a long list / dictionary / string / numeric / genuine]
Why do they do:
-
In addition to convenience python interpreter to run code written in python
-
Other code written facilitate interpreter, it is converted into something recognizable python
Import json
# serialized, converted to the value of the python json format string
V = [ . 11, 22 is, . 4, { "K1": "V2"}, True, "SAF"]
V1 = json. dumps ( V)
Print ( V1) # deserialize the json python string format is converted to data type V2 = '[' Alex ', 123]' Print ( type ( V2)) V3 = json. loads ( V2) Print ( v3, of the type ( v3))
6.3 the
-
Size os.stat ( 'filename') .st_size read the file
import os
file_size = os.stat('刘丹妮.mp4').st_size
read_size = 0
with open('刘丹妮.mp4',mode = 'rb') as f1,open('a.mp4',mode='wb') as f2:
while read_size < file_size:
chunk = f1.read(1024)#每次最多读取1024字节
f2.write(chunk)
read_size += len(chunk)
val = int(read_size/file_size*100)
print('%s%%/r' %val,end="")
-
os.path.exists (path), if the path exists, returns True; if the path does not exist, returns False
-
os.path.abspath (), get a file absolute path
path = '20190409_192149.mp4'
import os
v1 = os.path.abspath(path)
print(v1)
-
os.path.dirname, get the parent directory path
-
import os
v = r"D:\code\s21day14\20190409_192149.mp4"
print(os.path.dirname(v)) -
Stitching os.path.join, path
import os
path = "D:\code\s21day14" # user/index/inx/fasd/
v = 'n.txt'
result = os.path.join(path,v)
print(result)
result = os.path.join(path,'n1','n2','n3')
print(result)
-
os.listdir, to view a directory of all the files [first floor]
-
import os
result = os.listdir(r'D:\code\s21day14')
for path in result:
print(path) -
os.walk, to view a directory of all the files [all levels]
Import os the Result = os. Walk ( r'D: \ code \ s21day14 ') for A, b, c in the Result: # A, are viewing the directory b, files in this directory folder c, files in this directory for Item in C: path = OS. path. the Join ( A, Item) Print ( path)
-
os.makedirs, create directories and subdirectories
import os
file_path = r'db/sf/sf/er/xxx.txt'
file_folder = os.patn.dirname(file_path)
if not os.path.exists(file_path)
os.makedirs(file_folder)
with open(file_path,mode='w',encoding='utf-8') as f:
f.write('asdf')
-
os.rename, rename
import the
them. rename ( 'sb', '250')
6.4 Special supplement
= v1 r "D: \ code \ s21day14 \ n1.mp4" ( recommended)
Print ( v1) v2 = "D: \\ code \\ \\ n1.mp4 s21day14" Print ( v2) #python in execution process, the \ very sensitive, according to the case to write, it is easy on the error system, is about to make the role of r \ are considered merely \, to distinguish \ n this
-
shutil
import statement the shutil
the shutil. rmtree ( path)