unit00 2020-05-14
路径处理,需导入 os包
import os # 导入 os 包 """获取当前运行文件所在文件夹的路径01""" current_dir = os.getcwd() # 无参数 print(current_dir) # 当前运行文件的上一级目录,输出:D:\soft\pycharm\Project_Script\基础 """绝对路径""" print(os.path.abspath(__file__)) print(os.path.realpath(__file__)) # 都输出:D:\soft\pycharm\Project_Script\基础\路径处理.py """获取当前运行文件所在文件夹的路径02""" path = os.path.abspath(__file__) print(os.path.dirname(path)) # 输出:D:\soft\pycharm\Project_Script\基础
路径拼接 os.path.join(path, ‘data‘)
import os # 获取当前文件的上一级目录 path_dir = os.path.dirname(os.path.abspath(__file__)) # path_dir = os.getcwd() # 路径拼接 data_path = os.path.join(path_dir, ‘data‘) print(data_path) # 输出:D:\soft\pycharm\Project_Script\基础\data
创建目录