python常用模块2

it2022-05-05  140

os模块 print(os.getcwd())#取当前工作目录 print(os.chdir(r"e:\byz_code\day2"))#更改当前目录 print(os.mkdir("test1"))#创建文件夹 print(os.makedirs(r"test1\test2"))#递归创建文件夹,父目录不存在时创建父目录 # print(os.removedirs(r"test1\test2"))#递归删除空目录 # print(os.rmdir("test1"))#删除指定的文件夹,只能删除空文件夹 print(os.remove(r"E:\byz_code\day4\a.txt"))#删除文件 # os.rename("test","test1")#重命名 # print(os.sep)#当前操作系统的路径分隔符 print(__file__)#代表当前文件 print(os.path.abspath('bb.py'))#获取绝对路径 # print(__file__)#代表当前文件 # print(os.path.dirname) # print(os.path.dirname(os.path.dirname(__file__)))#获取父目录 # print(os.path.exists("hhaaa"))#目录/文件是否存在 # print(os.path.isfile("bb.py"))#判断是否是一个文件 # print(os.path.isdir("/usr/local"))#是否是一个路径 # print(os.path.join("root",'hehe','haha','a.log'))

time&datetime模块time和datetime模块主要用于操作时间时间有三种表示方式,一种是时间戳、一种是格式化时间、一种是时间元组

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15          import datetime , time          print ( time . timezone ) #和标准时间相差的时间,单位是s      print ( time . time ( ) ) #获取当前时间戳      print ( time . sleep ( 1 ) ) #休息几s      print ( time . gmtime ( ) ) #把时间戳转换成时间元组,如果不传的话,默认取标准时区的时间戳      print ( time . localtime ( ) ) #把时间戳转换成时间元组,如果不传的话,默认取当前时区的时间戳      print ( time . mktime ( time . localtime ( ) ) ) #把时间元组转换成时间戳      print ( time . strftime ( "%y%m%d %H%M%S" ) ) #将时间元组转换成格式化输出的字符串      print ( time . strptime ( "20160204 191919" , "%Y%m%d %H%M%S" ) ) #将格式化的时间转换成时间元组      print ( time . struct_time ) #时间元组      print ( time . asctime ( ) ) #时间元转换成格式化时间      print ( time . ctime ( ) ) #时间戳转换成格式化时间      print ( datetime . datetime . now ( ) ) #当然时间格式化输出      print ( datetime . datetime . now ( ) + datetime . timedelta ( 3 ) ) #3天后的时间      print ( datetime . datetime . now ( ) + datetime . timedelta ( - 3 ) ) #3天前的时间

re模块re模块是正则表达式模块,用来匹配一些特定的字符串。常用的正则表达式符号

  1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17          '.'     默认匹配除 \ n之外的任意一个字符,若指定 flag  DOTALL ,则匹配任意字符,包括换行          '^'     匹配字符开头,若指定 flags  MULTILINE ,这种也可以匹配上 ( r "^a" , "\nabc\neee" , flags = re . MULTILINE )          '$'     匹配字符结尾,或 e . search ( "foo$" , "bfoo\nsdfsf" , flags = re . MULTILINE ) . group ( )也可以          '*'     匹配 *号前的字符 0次或多次, re . findall ( "ab*" , "cabb3abcbbac" )  结果为 [ 'abb' ,  'ab' ,  'a' ]          '+'     匹配前一个字符 1次或多次, re . findall ( "ab+" , "ab+cd+abb+bba" ) 结果 [ 'ab' ,  'abb' ]          '?'     匹配前一个字符 1次或 0次          '{m}'   匹配前一个字符 m次          '{n,m}' 匹配前一个字符 nm次, re . findall ( "ab{1,3}" , "abb abc abbcbbb" ) 结果 'abb' ,  'ab' ,  'abb' ]          '|'     匹配 |左或 |右的字符, re . search ( "abc|ABC" , "ABCBabcCD" ) . group ( ) 结果 'ABC'          '(...)' 分组匹配, re . search ( "(abc){2}a(123|456)c" ,  "abcabca456c" ) . group ( ) 结果  abcabca456c          '\A'    只从字符开头匹配, re . search ( "\Aabc" , "alexabc" ) 是匹配不到的          '\Z'    匹配字符结尾,同 $          '\d'    匹配数字 0 - 9          '\D'    匹配非数字          '\w'    匹配 [ A - Za - z0 - 9 ]          '\W'    匹配非 [ A - Za - z0 - 9 ]          's'     匹配空白字符、 \ t\ n\ r  ,  re . search ( "\s+" , "ab\tc1\n3" ) . group ( ) 结果  '\t'

常用的匹配语法

  1 2 3 4 5          re . match 从头开始匹配                  re . search 匹配包含          re . findall 把所有匹配到的字符放到以列表中的元素返回          re . splitall 以匹配到的字符当做列表分隔符          re . sub      匹配字符并替换

 

 

转载于:https://www.cnblogs.com/lazy-cat-home/p/7072542.html


最新回复(0)