Home  >  Q&A  >  body text

python 如何获取调用脚本的真实路径?

这我的文件结果,但是我在命令行直接运行命令的时候,或是在别的地方调用脚本的时候,总是找不到文件?

BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
print (os.path.join(BASE_DIR,"排名.xls"))

df = read_excel(os.path.join("排名.xls"))

这个脚本不行~~在当前目录下是可以的,但是 退一级目录就不行了

天蓬老师天蓬老师2741 days ago499

reply all(2)I'll reply

  • PHPz

    PHPz2017-04-18 10:28:29

    I didn’t realize you used 2os.path.dirname嘛?BASE_DIR 为/百度覆盖率,
    而"排名.xls"的路径在/百度覆盖率/官网业务词目录下,os.path.join(BASE_DIR,"排名.xls")Of course they don’t exist

    reply
    0
  • 怪我咯

    怪我咯2017-04-18 10:28:29

    It is estimated that if you are using Python2 on the windows platform, you will have this problem with Chinese file names

    It is recommended to upgrade Python3 to solve...

    reply
    0
  • Cancelreply