我想解析一个excel数据文件,并希望将其制作出来,以便我的程序根据我正在使用的当前python文件的文件位置自动填写文件路径。

例如,在代码中

categorization_file = r'C:\Users\Name\Desktop\ExcelFile.xlsx'
inputVariables = categorization_file.parse(sheet_name='Control')


我希望在可能的情况下自动生成“ r'C:\ Users \ Name \ Desktop \”部分。该路径与我从中运行程序的文件相同。

谢谢

最佳答案

import os
# to get the location of the current python file
basedir = os.path.dirname(os.path.abspath(__file__))
# to join it with the filename
categorization_file = os.path.join(basedir,'ExcelFile.xlsx')

关于python - 如何在python中自动找到文件路径,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/56549732/

10-14 17:58
查看更多