__file__ vs __FILE__ [英] __file__ vs __FILE__

查看:97
本文介绍了__file__ vs __FILE__的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我从这个角度提前道歉但是...


在PHP中你有__FILE__常量,它给你的价值
$ b你所在文件的$ b绝对路径(与主脚本

文件相对。)使用函数dirname,这样可以轻松获得

该文件中特定文件的父目录:


$ parent_dir = dirname(__ FILE__);


我正在寻找在Python中实现这一目标的最佳方法。这似乎是工作的



parent_dir = os.path.normpath(os.path.join(os.path.abspath(__ fi le __),

''..''))


任何人都可以确认这种方法的可靠性或建议更好的

(一个 - line)实现这个的方法吗?


谢谢,

Tom

I apologize in advance for coming at this from this angle but...

In PHP you have the __FILE__ constant which gives you the value of the
absolute path of the file you''re in (as opposed to the main script
file.) With the function dirname, this makes it easy to get the
parent dir of a particular file from within that file:

$parent_dir = dirname(__FILE__);

I''m looking for the best way to accomplish this in Python. This seems
to work:

parent_dir = os.path.normpath(os.path.join(os.path.abspath(__fi le__),
''..''))

Can anyone confirm the reliability of this method or suggest a better
(one-line) method for accomplishing this?

Thanks,
Tom

推荐答案

parent_dir = dirname(__ FILE__);


我正在寻找在Python中实现这一目标的最佳方法。这似乎是工作的



parent_dir = os.path.normpath(os.path.join(os.path.abspath(__ fi le __),

''..''))


任何人都可以确认这种方法的可靠性或建议更好的

(一个 - line)实现这个的方法吗?


谢谢,

Tom

parent_dir = dirname(__FILE__);

I''m looking for the best way to accomplish this in Python. This seems
to work:

parent_dir = os.path.normpath(os.path.join(os.path.abspath(__fi le__),
''..''))

Can anyone confirm the reliability of this method or suggest a better
(one-line) method for accomplishing this?

Thanks,
Tom


从文件运行脚本时会出现__file__属性。

如果从交互式解释器运行,它将引发一个

NameError。同样,我相信在早期版本的Python

(2.1?Pre 2.2?)中,它只在导入的模块中设置。我用几个脚本

自己使用了

''os.path.realpath(os.path.pardir)''构造。这应该在交互式翻译中工作。


Jeff


2007年11月2日晚上11:21,klenwell写道:
The __file__ attribute is present when you run a script from a file.
If you run from the interactive interpreter, it will raise a
NameError. Likewise, I believe that in earlier versions of Python
(2.1? Pre 2.2?) it was only set within imported modules. I''ve used the
''os.path.realpath(os.path.pardir)'' construct in a couple of scripts
myself. That ought to work within the interactive interpreter.

Jeff

On Nov 2, 2007, at 11:21 PM, klenwell wrote:

我从这个角度提前道歉但是......


在PHP中你有__FILE__常量给出你所在文件的

绝对路径的值(而不是主脚本

文件。)使用函数dirname,这使它成为很容易从该文件中获取特定文件的父目录:

I apologize in advance for coming at this from this angle but...

In PHP you have the __FILE__ constant which gives you the value of the
absolute path of the file you''re in (as opposed to the main script
file.) With the function dirname, this makes it easy to get the
parent dir of a particular file from within that file:


parent_dir = dirname(__ FILE __);


我正在寻找在Python中实现这一目标的最佳方法。这似乎是工作的



parent_dir = os.path.normpath(os.path.join(os.path.abspath(__ fi le __),

''..''))


任何人都可以确认这种方法的可靠性或建议更好的

(一个 - line)实现这个的方法吗?


谢谢,

Tom


-
http://mail.python.org/mailman/listinfo/python-list
parent_dir = dirname(__FILE__);

I''m looking for the best way to accomplish this in Python. This seems
to work:

parent_dir = os.path.normpath(os.path.join(os.path.abspath(__fi le__),
''..''))

Can anyone confirm the reliability of this method or suggest a better
(one-line) method for accomplishing this?

Thanks,
Tom

--
http://mail.python.org/mailman/listinfo/python-list


这篇关于__file__ vs __FILE__的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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