为os.path.join指定部分路径

时间:2017-08-29 12:56:26

标签: python python-3.x

我有一个python脚本,可以创建PDF并将其保存在保存脚本的文件夹的子文件夹中。我有以下将文件保存到子文件夹:

outfilename = "Test" + ".pdf" #in real code there is a var that holds the name of the file
outfiledir = 'C:/Users/JohnDoe/Desktop/dev/PARENTFOLDER/SUBFOLDER/' #parent folder is where the script is - subfolder is where the PDFs get saved to
outfilepath = os.path.join(outfiledir, outfilename)

有没有办法将PDF保存到子文件夹而无需指定完整路径?让我们说我想让你的脚本成为多台计算机可以使用的exe,我如何显示路径以便将PDF保存在subfoler中?

谢谢!

1 个答案:

答案 0 :(得分:4)

试一试:

import os
dir_name = os.path.dirname(os.path.abspath(__file__)) + '/subdir'
path = os.path.join(dir_name, 'filename')
相关问题