<>方法一:
项目名:InterfaceTest_master,根目录:InterfaceTest_master/
#获取当前文件的目录 cur_path = os.path.abspath(os.path.dirname(__file__)) # 获取根目录
root_path= cur_path[:cur_path.find("InterfaceTest_master\\")+len(
"InterfaceTest_master\\")] print(root_path) # 创建其他目录 data_path = os.path.abspath
(root_path +'\\excelReport') if not os.path.exists(data_path): os.mkdir(
data_path) # 如果不存在这个excelReport文件夹,就自动创建一个 print(data_path)
<>运行结果:
C:\Users\Administrator\Desktop\InterfaceTest_master\ C:
\Users\Administrator\Desktop\InterfaceTest_master\excelReport
<>方法二:
# 获取根目录 base_dir = dirname(dirname(abspath(__file__))) print(base_dir) #
修改成linux目录 base_dir = base_dir.replace('\\', '/') print(base_dir) # 创建其他目录
file_path= base_dir + "/excelReport" print(file_path)
<>运行结果:
C:\Users\Administrator\Desktop\InterfaceTest_master C:/Users/Administrator/
Desktop/InterfaceTest_master C:/Users/Administrator/Desktop/InterfaceTest_master
/excelReport