A module

A, os module

os module is responsible for programs interact with the operating system, provides the interface to access the underlying operating system, used for file handling, control files and folders.

First import the os module

import os

Manipulate files

os.path.isfile(r' ')  判断是否为文件
os.remove(r' ')   删除文件
os.rename(r' ', r' ')  重命名文件

Manipulate files folder

os.path.isdir() 判断是否为文件夹
os.mkdir(r' ')  创建文件夹
os.rmdir(r' ')  删除文件夹
os.listdir(r' ')    列出文件夹内所有的文件(*)

Accessibility

os.getced () where the file currently executing file folder

print (__ file __) or os.path.abspath (__ file __) of the current path of the file where the specific

File folders:

res = os.path.dirname(os.path.dirname(os.path.abspath(file)))
print(res)

Splicing file path:

res = os.path.join(os.path.dirname(os.path.abspath(file)), 'img', 'test.jpg')
print(res)

Determine whether there is a path (file or folder apply):

res = os.path.exists (r'D: \ Shanghai python12 of video \ python12 of video \ day 16 \ 01 os module .py ')
Print (RES)

Operating files, try not to write path

Two, sys module

Responsible for interacting with the python interpreter program sys module, it provides a range of functions and variables to control when running python environment.

  1. Most commonly, when the python interpreter to run the command line, the use of the extra parameters

    res = sys.argv
    print(res)
  2. Modules currently get Imported

    print(sys.modules.keys())
  3. To understanding

    print(sys.api_version)
    print(sys.copyright)
    print(sys.version)
    print(sys.hexversion)

Three, json module; cross-platform data exchange, json string

SUMMARY json conversion module may have

pickle module using the same method as the json module

  1. Serialization; converting python code string format json

    dic=[1,2,3]  
    res=json.dumps(dic)  # 存储在内存中
    print(type(res),res)
    # 将字典存储到.json文件中
    dic = {'a':True,'b':Fales}
    import json
    with open('text.json','w',encoding='utf-8') as fw:
      json.dump(dic,fw)
  2. Deserialization; json data is converted to the format for the data type python

    res = json.loads(res)  # 存储在内存中
    print(type(res), res)
    #将文件中的json窜读取出来
    with open('test.json', 'r', encoding='utf8') as fr:
        dic = json.load(fr)
        print(type(dic), dic)
    Json function with memory storage function, then only the object, not the contents of the storage function

Four, logging module; generate log

  1. Object Configuration logger

    import logger
    nick_logger = logging.Logger('nick')
    json_logger = logging.Logger('jason')
  2. Configuration log format

    formmater1 = logging.Formatter('%(asctime)s - %(name)s -%(thread)d - %(levelname)s -%(module)s:  %(message)s',
                                   datefmt='%Y-%m-%d %H:%M:%S %p ', )
    formmater2 = logging.Formatter('%(asctime)s :  %(message)s',
                                   datefmt='%Y-%m-%d %H:%M:%S %p', )
    formmater3 = logging.Formatter('%(name)s %(message)s', )
  3. Configuration handler -> to print or documents to the terminal Print

    h1 = logging.FileHandler('nick.log')
    h2 = logging.FileHandler('json.log')
    sm = logging.StreamHandler()
  4. The handler bind to target logger

  5. nick_logger.addHandler(h1)
    nick_logger.addHandler(sm)
    json_logger.addHandler(h2)

Guess you like

Origin www.cnblogs.com/allenchen168/p/11601007.html