当前文件的路径取决于我如何执行程序 [英] Path to current file depends on how I execute the program

查看:63
本文介绍了当前文件的路径取决于我如何执行程序的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的Python程序:

This is my Python program:

#!/usr/bin/env python

import os

BASE_PATH = os.path.dirname(__file__)
print BASE_PATH

如果我使用python myfile.py运行它,则会打印一个空字符串.如果我使用myfile.py运行它,它将打印正确的路径.为什么是这样?我正在使用Windows Vista和Python 2.6.2.

If I run this using python myfile.py it prints an empty string. If I run it using myfile.py, it prints the correct path. Why is this? I'm using Windows Vista and Python 2.6.2.

推荐答案

这只是一个无害的Windows怪癖;您可以使用os.path.abspath(__file__)进行补偿,请参见文档

It's just a harmless windows quirk; you can compensate by using os.path.abspath(__file__), see the docs

这篇关于当前文件的路径取决于我如何执行程序的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆