python中os、sys、hashlib、logging、configparser模块
os模块
调用:import os
os.getcwd()无需传入参数,返回当前脚本所在的路径。D:\高级数据类型\线程
os.chdir(r"D:\高级数据类型")传入一个字符串类型的地址,改变当前脚本所在路径,无返回。
os.curdir不带括号,返回当前工作目录。
os.pardir不带括号,返回当前工作目录的父级工作目录
os.makedirs(r"abc\artisan")在当前工作目录生成abc文件夹,如果使用os.chdir(),则会在新的路径下生成abc文件夹,如果要创建的文件夹已经存在,则程序会抱错。
os.removedirs(r"abc\artisan")删除文件夹,abc下的artisan文件夹及abc文件夹。如果文件夹内有内容,则不能删除,removedirs()只能删除空文件,如果删除的路径不存在,则会报错。
os.makedir("abc")不能嵌套生成文件夹,可先生成abc文件夹;再使用os.makedir(r"abc\artisan")创建artisan文件夹。
os.rmdir("abc")删除空的单个文件夹。
os.listdir()不传入参数时,返回当前脚本所在目录下的所有文件(包括扩展名)的列表;如果传入绝对路径参数,则返回绝对路径下的所有文件名称列表。
os.remove('wenjian.txt")只能删除名称为英文的文件。只能删除文件,不能删除文件夹。
os.rename("old", "new")能修改文件的名字,文件名不需要带扩展名;此函数也能修改文件夹名称。
os.stat(".")传入路径参数或者文件的参数,返回当前文件夹所在的信息,或者是文件的信息,能获取文件夹或者文件的大小,创建时间,用于断点续传。atime最后一次访问时间。
os.sep不需要传入参数,返回当前系统路径的分隔符。
os.linesep不需要传入参数,返回当前系统的换行分割符。windows是\r\n换行,Linux是\n
os.pathsep不需要传入参数,返回当前系统的环境变量的分割符,Windows是分好;
os.name不需要传入参数,返回当前使用的系统
os.system(“dir”)传入字符串类型的命令行参数,返回如同命令窗口的结果。
os.environ返回当前系统中的环境变量,返回的是字典类型。
os.path.abspath("abc")传入字符串类型文件名的参数,返回绝对路径。
os.path.split(os.path.abspath("abc"))传入字符串类型文件的绝对路径,返回两个元素的元组,元素的类型的是字符串。
os.path.dirname("abc")返回文件abc所在的文件夹路径。返回的数据类型是字符串。
os.path.basename(“”)传入路径参数,返回带有扩展名的文件名。
os.path.exists(path)判断路径是否存在,返回布尔值
os.path.isabs(path)判断是否是绝对路径,返回布尔值
os.path.isifile(path)传入文件的整个路径参数,判断文件是否存在,返回布尔值
os.path.isdir(path)判断是否存在的目录,返回布尔值
os.path.jion(path1[, path2]),将多个路径组合后返回
os.path.getatime(path)返回path所指向的文件或者目录最后保存的时间
os.path.getmtime(path)返回path所指向的文件或者目录最后修改的时间
sys模块
调用:import sys
sys.argv命令行参数list,第一个是程序本身路径
sys.exit()退出程序
sys.version获取python解释程序版本的信息
sys.int 返回最大的int值
sys.path返回模块搜索的路径,初始化使用pythonpath环境变量,返回的数据结构是列表。
sys.platform返回操作系统平台名称,Windows是win32
sys.stdout.write("hello")直接输出的显示台
sys.stdin.readline()返回序列
hashlib模块
m = hashlib.md5(),创建此模块对象
m.update("加密内容".encode("utf-8"))对选择的内容进行加密,update()函数能连续加密,例如m.update("123"),此时返回额结果如同m.update("加密内容123".encode())。
m.hexdigest(),不需要传入参数,直接调用返回加密后的对象,用16进制表示。
logging模块
logging.debug("debug message") # 默认不输出
logging.info("info message") # 默认不输出
logging.waning("waning message")
longging.error("error message")
longging.critical("critical message")
日志的等级配置
1 import logging 2 3 # 创建logger对象 4 logger = logging.getLogger() 5 6 # 文件输出流对象,输出到文件 7 fh = logging.FileHandler("test.log") 8 9 # 信息输出到控制台对象 10 ch = logging.StreamHandler() 11 12 # 创建格式对象 13 formatter = logging.Formatter('%(asctime)s - %(name)s - %(levelname)s - %(message)s') 14 15 16 # 文件输出取出标准输出格式 17 fh.setFormatter(formatter) 18 19 # 屏幕输出也需要标准输出格式 20 ch.setFormatter(formatter) 21 22 logger.addHandler(fh) 23 logger.addHandler(ch) 24 25 logger.setLevel(logging.DEBUG) # 修改日志显示的的级别 26 27 logger.debug("logger debug message") 28 logger.info("logger info message") 29 logger.warning("logger warning message") 30 logger.error("logger error message") 31 logger.critical("logger critical message")
1 import logging 2 3 logging.basicConfig(level=logging.DEBUG, 4 format='%(asctime)s %(filename)s[line:%(lineno)d] %(levelname)s %(message)s', 5 datefmt='%a, %d, %b %Y %H:%M:%S', 6 filename='myapp.log', 7 filemode='w' 8 ) 9 10 logging.debug('this is debug message') 11 logging.info('this is info message') 12 logging.warning('this is warning message') 13 14 # filename 指定日志文件名 15 # filemode 指定日志文件的打开模式,w或者a 16 # format 指定输出的格式和内容,format可以输出很多有用信息, 17 # %(levelno)s 打印日志级别的数值 18 # %(levelname)s 打印日志级别名称 19 # %(pathname)s 打印当前执行程序的路径,其实就是sys.argv[0] 20 # %(filename)s 打印当前执行程序名 21 # %(funcName)s 打印日志的当前函数 22 # %(lineno)d 打印日志的当前行号 23 # %(asctime)s 打印日志的时间 24 # %(thread)d 打印线程ID 25 # %(threadName)s 打印线程名称 26 # %(process)d 打印进程ID 27 # %(message)s 打印日志信息 28 # datefmt 指定时间格式,同time.strftime() 29 # level 设置日志级别,默认为logging.WARNING 30 # stream 指定将日志的输出流,可以指定输出到sys.stderr,sys.stdout或者文件,默认输出到sys.stderr
ConfigParser模块
配置文件的作用:将代码模块化,不同的代码做不同的事情。
创建配置的文件
1 import configparser 2 3 config = configparser.ConfigParser() # 取得文配置的对象 4 5 config["DEFAULT"] = { 6 "ServerAliveInterval":"45", 7 "Compression":"yes", 8 "CompressionLevel":"9" 9 } 10 config["bitbucket.org"] = {"User":"hg"} 11 config['topsecret.server.com'] = {} 12 topsecret = config['topsecret.server.com'] 13 topsecret['Host Port'] = '50022' 14 topsecret['ForwardX11'] = 'no' 15 config['DEFAULT']['ForwardX11'] = 'yes' 16 17 with open('example.ini', 'w') as configfile: 18 config.write(configfile)
读配置的文件
1 config.read("example.ini") 2 print(config.sections()) # 默认DEFAULT不会显示 3 print(config.defaults()) # 打开DEFAULT的方法 4 print("bitbucket.org" in config) # 判断"bitbucket.org"是否在config里面,config应该是字典类型 5 print(config["bitbucket.org"]['User']) # 取值 6 7 for key in config["bitbucket.org"]: 8 print(key)
修改配置文件
1 config.read("example.ini") 2 3 config.remove_section('topsecret.server.com') # 删除 4 config.remove_option("bitbucket.org","User") # 删除 5 6 print(config.has_section('topsecret.server.com')) # 判断是否有这个模块 7 8 config.set("bitbucket.org","User", "alex") # 修改 9 10 config.write(open('i.cfg', "w")) # 重新写入
浙公网安备 33010602011771号