先调用os模块:import os
1、sep():获取系统分隔符
print(os.sep)
2、getcwd():获取当前工作目录
print(os.getcwd())
3、listdir():查询目录下的文件列表
dir = "D:\\自考教材资料"
print(os.listdir(dir))
4、mkdir():创建目录文件
os.mkdir("testcase")
5、rmdir():删除一个空目录
os.rmdir("testcase")
6、remove():删除指定文件
os.remove("D:\\123.txt")
7、rename():重命名目录或文件
os.rename("D:\\123.txt","D:\\123456.txt")
8、__file__:获取python脚本文件的全路径
print(__file__)
9、os.path.dirname():返回文件所在目录
path = os.path.dirname(__file__)
print(path)
print(os.path.dirname(path))#获取所在目录的上一级目录
10、os.path.abspath():返回文件的绝对路径
path = os.path.abspath(__file__)
print(path)
11、组合dirname()和abspath()
path = os.path.dirname(os.path.abspath(__file__))
print(path)
12、os.path.join():路径拼接
path1 = os.path.join(os.path.dirname(__file__),"Test.py")
print(path1)
path2 = os.path.join(os.path.dirname(os.path.abspath(__file__)),"Test.py")
print(path2)
13、os.path.exists():判断是否存在;os.path.isfile:判断是否为文件;os.path.isdir:判断是否为目录
conf_path = os.path.dirname(os.path.abspath(__file__))
conf_file = os.path.join(conf_path,"score001.xlsx")
res_file = os.path.exists(conf_file)
print(res_file)
is_file = os.path.isfile(conf_file)
print(is_file)
res_dir = os.path.exists(conf_path)
print(res_dir)
is_dir = os.path.isdir(conf_path)
print(is_dir)