Python之路径处理
路径处理,需导入 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
创建目录
相关推荐
YENCSDN 2020-11-17
lsjweiyi 2020-11-17
houmenghu 2020-11-17
Erick 2020-11-17
HeyShHeyou 2020-11-17
以梦为马不负韶华 2020-10-20
lhtzbj 2020-11-17
夜斗不是神 2020-11-17
pythonjw 2020-11-17
dingwun 2020-11-16
lhxxhl 2020-11-16
坚持是一种品质 2020-11-16
染血白衣 2020-11-16
huavhuahua 2020-11-20
meylovezn 2020-11-20
逍遥友 2020-11-20
weiiron 2020-11-16