OS模块

  1. os.name:
    操作系统类型

  2. os.environ:
    操作系统的环境变量

  3. os.environ.get(‘PROMPT’):
    获取指定环境变量

  4. os.curdir:
    获取当前目录

  5. os.getcwd()
    获取当前工作目录,即当前python脚本所在的目录

  6. os.listdir(r’D:\trainingfile’):
    以列表形式返回指定目录下的所有文件

  7. os.mkdir(‘hyl’):
    创建目录

  8. os.rmdir(‘hyl’):
    删除目录

  9. os.stat(‘hyl’):
    获取文件属性

  10. os.rename(‘hyl’,’hyl2’):
    重命名

  11. os.remove(‘hyl.txt’):
    删除文件

  12. os.system():
    运行命令

    1
    2
    # 关闭进程
    os.system('taskkill /f /im notepad.exe')
  13. os.path.abspath(‘.liangbo’):
    获取当前的绝对路径

  14. os.path.join(p1,p2):
    拼接路径
    注意:p2开始不能有反斜杠

  15. os.path.split(p1):
    拆分路径

    1
    2
    print(os.path.split(r'F:\乱七八糟\360安全浏览器下载\chrome\下载\messing\apk\2.1.0.4_picacg.apk'))
    # ('F:\\乱七八糟\\360安全浏览器下载\\chrome\\下载\\messing\\apk', '2.1.0.4_picacg.apk')
  16. os.path.splitext(p1):
    获取扩展名

    1
    2
    print(os.path.splitext(r'F:\乱七八糟\360安全浏览器下载\chrome\下载\messing\apk\2.1.0.4_picacg.apk'))
    # ('F:\\乱七八糟\\360安全浏览器下载\\chrome\\下载\\messing\\apk\\2.1.0.4_picacg', '.apk')
  17. os.path.isdir:
    是否为目录

  18. os.path.isfile(p1)
    判断文件是否存在

  19. os.path.exists()
    判断文件是否存在

  20. os.path.getsize(path3):
    获取文件大小

  21. 获取文件名
    os.path.dirname

递归

方式:

  1. 写出基线条件
  2. 找这一次和上一次的关系
  3. 假设当前函数已经能用,==调用自身计算上一次的结果==,再求出本次的结果
1
2
3
4
5
6
7
8
9
10
11
# 递归调用,打印目录下所有文件
def find_all_dir(path,sp=' '):
files_list = os.listdir(path)
sp += ' '
for file_name in files_list:
fileAbsPath = os.path.join(path,file_name)
if os.path.isdir(fileAbsPath):
print('目录:',file_name)
find_all_dir(fileAbsPath)
else:
print('文件:',file_name)

如果要用深度变量,一般都是使用==栈==,

目录树

思路:每层压入,然后弹出

  1. 压入A,弹出A(弹出的同时获取A下面的文件夹B和C)
  2. 压入BC
  3. 弹出B(弹出的同时获取B下面的文件夹D和E)
  4. 压入DE
  5. 弹出D(弹出的同时获取D下面的文件夹H和I)
  6. 弹出H
    所以现在的栈就是[c,e,i](i为栈顶)
  7. 弹出I
  8. 弹出E(弹出的同时获取E下面的文件夹J和K)
  9. 弹出J
  10. 弹出K
  11. 弹出C(弹出的同时获取C下面的文件夹F和G)
  12. 弹出F
  13. 弹出G
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
def find_all_dir(path):
stack = []
stack.append(path)

while len(stack) != 0:
dirPath = stack.pop()
file_list = os.listdir(dirPath)

for filename in file_list:
fileAbsPath = os.path.join(dirPath,filename)
if os.path.join.isdir(fileAbsPath):
print('目录:',filename)
stack.append(fileAbsPath)
else:
print('文件:',filename)

如果使用广度优先:

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
import os
import collections

def getAllDir(path):
queue = collections.deque()
queue.append(path)

while len(queue) != 0:
dirpath = queue.popleft()
fileslist = os.listdir(dirpath)

for filename in fileslist:
fileAbsPath = os.path.join(dirpath,filename)

if os.path.isdir(fileAbsPath):
print('目录:',filename)
queue.append(fileAbsPath)
else:
print('文件:',filename)

时间模块:

  1. 时间戳:
    以整形或浮点型表示时间的一个以秒为单位的时间间隔
  2. 元组:
    这个元组有9个整形内容:
    (year,month,day,hours,minutes,seconds,weekdey,julia day,DST)
  3. 格式化字符串
  1. time.time():
    返回当前时间的时间戳(浮点数形式)

  2. time.gmtime:
    将时间戳转为UTC时间元组:

    1
    2
    3
    4
    5
    6
    7
    8
    import time

    c = time.time()
    x = time.gmtime(c)
    print(x)

    # 注意是格林尼治时间
    # time.struct_time(tm_year=2019, tm_mon=4, tm_mday=22, tm_hour=8, tm_min=44, tm_sec=58, tm_wday=0, tm_yday=112, tm_isdst=0)
  3. time.localtime():
    转为当地时间的元组:

    1
    2
    3
    4
    5
    6
    import time

    c = time.time()
    x = time.localtime(c)
    print(x)
    # time.struct_time(tm_year=2019, tm_mon=4, tm_mday=22, tm_hour=16, tm_min=47, tm_sec=5, tm_wday=0, tm_yday=112, tm_isdst=0)
  4. time.mktime:
    将本地时间转为时间元组

    1
    2
    3
    4
    5
    6
    7
    8
    import time
    a = time.time()
    b = time.localtime(a)

    m = time.mktime(b)
    # 注意会省略小数
    print(m) # 1555922992.0
    print(a) # 1555922992.8914602
  5. time.asctime():
    将时间元组转成字符串

    1
    2
    3
    4
    5
    import time

    a = time.localtime()
    x = time.asctime(a)
    print(x) # Mon Apr 22 16:52:30 2019
  6. time.ctime():
    将时间戳转为字符串

    1
    2
    3
    4
    5
    import time

    a = time.time()
    x = time.ctime(a)
    print(x) # Mon Apr 22 16:54:24 2019
  7. time.strftime():
    将时间转成特定格式的字符串:

    1
    2
    a = time.localtime()
    time.strftime('%Y-%m-%d %H:%M:%S',a)
  8. time.strptime()
    将时间字符串转为时间元组

1555925969119

  1. time.sleep():
    延迟时间
  2. time.clock:
    返回当前程序的cup执行时间

datetime是基于time进行了封装,提供了更为使用的函数.接口更直观,更容易调用.

datetime的五个类:

  1. datetime:
    同时有日期和时间
  2. timedelta
    主要用于计算时间的跨度
  3. tzinfo
    时区相关
  4. time
    只关注时间
  5. date
    只关注日期
  1. datetime.datetime.now():
    获取当前时间
  2. datetime.datetime(1999,9,9,10,28,25,123456):
    获取指定时间