1.当前路径及路径下的文件
os.getcwd():查看当前所在路径。
os.listdir(path):列举目录下的所有文件。返回的是列表类型。
#-*-coding:utf-8-*-
import os
print (os.getcwd())
print (os.listdir(os.getcwd()))
E:\python36\python3.exe E:/pj/request3/test2.py
E:\pj\request3
['.idea', 'data.ini', 'data1.ini', 'request3.iml', 'sqlConnectionPool.py', 'sqldb.py', 'test.py', 'test1.py', 'test2.py', 'test3.py', '__init__.py', '__pycache__']
Process finished with exit code 0
2.绝对路径
os.path.abspath(path):返回path的绝对路径。
#-*-coding:utf-8-*-
import os
print (os.path.abspath("."))
print (os.path.abspath(".."))
3.查看路径的文件夹部分和文件名部分
os.path.split(path):将路径分解为(文件夹,文件名),返回的是元组类型。os.path.join(path1,path2,...):将path进行组合,若其中有绝对路径,则之前的path将被删除。
#-*-coding:utf-8-*-
import os
print (os.path.split(r"D:\pj\request1\test_report\result18-06-0419-41-23.html"))
print (os.path.join(os.path.abspath('.'),'test2.py'))
E:\python36\python3.exe E:/pj/request3/test2.py
('D:\\pj\\request1\\test_report', 'result18-06-0419-41-23.html')
E:\pj\request3\test2.py
Process finished with exit code 0
os.path.dirname(path):返回path中的文件夹部分,结果不包含'\',os.path.basename(path):返回path中的文件名。
#-*-coding:utf-8-*-
import os
print (os.path.dirname(r"D:\pj\request1\test_report\result18-06-0419-41-23.html"))
print (os.path.basename(r"D:\pj\request1\test_report\result18-06-0419-41-23.html"))
E:\python36\python3.exe E:/pj/request3/test2.py
D:\pj\request1\test_report
result18-06-0419-41-23.html
Process finished with exit code 0
4.查看文件时间
os.path.getmtime(path):文件或文件夹的最后修改时间,从新纪元到访问时的秒数。
os.path.getatime(path):文件或文件夹的最后访问时间,从新纪元到访问时的秒数。
os.path.getctime(path):文件或文件夹的创建时间,从新纪元到访问时的秒数。
#-*-coding:utf-8-*-
import os
print (os.path.getmtime(r"D:\pj\request1\test_report\result18-06-0419-47-56.html"))
print (os.path.getatime(r"D:\pj\request1\test_report\result18-06-0419-47-56.html"))
print (os.path.getctime(r"D:\pj\request1\test_report\result18-06-0419-47-56.html"))
E:\python36\python3.exe E:/pj/request3/test2.py
1528112894.4665465
1528112895.0270474
1528112876.6849978
Process finished with exit code 0
5.查看文件大小
os.path.getsize(path):文件或文件夹的大小,单位字节
#-*-coding:utf-8-*-
import os
print (os.path.getsize(r"D:\pj\request1\test_report\result18-06-0419-47-56.html"))
E:\python36\python3.exe E:/pj/request3/test2.py
2860859
Process finished with exit code 0
6.查看文件是否存在
os.path.exists(path):文件或文件夹是否存在,返回True 或 False。
#-*-coding:utf-8-*-
import os
print (os.path.exists(r"D:\pj\request1\test_report"))
print (os.path.exists(r"D:\pj\request1\test_report1"))
print (os.path.exists(r"D:\pj\request1\test_report\result18-06-0419-47-56.html"))
print (os.path.exists(r"D:\pj\request1\test_report\result18-06-0419-47-561.html"))
E:\python36\python3.exe E:/pj/request3/test2.py
True
False
True
False
Process finished with exit code 0
7.实例说明
在自动化测试过程中,常常需要发送邮件,将最新的测试报告文档发送给相关人员查看,这是就需要查找最新文件的功能。
#-*-coding:utf-8-*-
import os
files={}
times=[]
path=r'D:\pj\request1\test_report'
filelist=os.listdir(path)
for file in filelist:
time=os.path.getmtime(os.path.join(path,file))
times.append(time)
files[time]=file
times.sort()
#print (times)
filelist=[]
for time in times:
filelist.append(files[time])
print (filelist)
E:\python36\python3.exe E:/pj/request3/test2.py
['result18-06-0419-41-23.html', 'result18-06-0419-42-08.html', 'result18-06-0419-47-56.html', 'result18-06-0419-48-14.html', 'result18-06-0514-37-14.html']
Process finished with exit code 0