本文介绍了__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

推荐答案




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

08-19 16:48