在python中引入文件路径的方法:1.使用os.path.abspath(_ _ file_ _)方法引入文件的绝对路径;2.使用os.path.abspath(sys.argv[0])方法引入文件的绝对路径;3.使用approot模块引入项目所在的绝对路径;
具体方法如下:
1.使用os.path.abspath(_ _ file_ _)方法引入文件的绝对路径
dirname, filename = os.path.split(os.path.abspath( _ _ file_ _))
os.path.realpath(_ _ file_ _)
2.使用os.path.abspath(sys.argv[0])方法引入文件的绝对路径
dirname, filename = os.path.split(os.path.abspath(sys.argv[0]))
os.path.realpath(sys.argv[0])
3.使用approot模块引入项目所在的绝对路径
import approot
print(approot.get_root()+'/FILE/file1.txt')
亿速云「云服务器」,即开即用、新一代英特尔至强铂金CPU、三副本存储NVMe SSD云盘,价格低至29元/月。点击查看>>
推荐阅读:mysql怎么引入脚本文件路径