What does __FILE__ mean?
The
realpath()
function gives you the file-system path, with any symbolic links and directory traversing (e.g. ../../) resolved. Thedirname()
function gives you just the directory, not the file within it.__FILE__
is a magic constant that gives you the filesystem path to the current .php file (the one that__FILE__
is in, not the one it's included by if it's an include.Sounds about right.
This is to translate from Windows style (\) paths to Unix style (/).
Is module __file__ attribute absolute or relative?
From the documentation:
__file__
is the pathname of the file from which the module was loaded, if it was loaded from a file. The__file__
attribute is not present for C modules that are statically linked into the interpreter; for extension modules loaded dynamically from a shared library, it is the pathname of the shared library file.
From the mailing list thread linked by @kindall in a comment to the question:
I haven't tried to repro this particular example, but the reason is
that we don't want to have to call getpwd() on every import nor do we
want to have some kind of in-process variable to cache the current
directory. (getpwd() is relatively slow and can sometimes fail
outright, and trying to cache it has a certain risk of being wrong.)What we do instead, is code in site.py that walks over the elements of
sys.path and turns them into absolute paths. However this code runs
before '' is inserted in the front of sys.path, so that the initial
value of sys.path is ''.
For the rest of this, consider sys.path
not to include ''
.
So, if you are outside the part of sys.path
that contains the module, you'll get an absolute path. If you are inside the part of sys.path
that contains the module, you'll get a relative path.
If you load a module in the current directory, and the current directory isn't in sys.path
, you'll get an absolute path.
If you load a module in the current directory, and the current directory is in sys.path
, you'll get a relative path.
Why does python __file__ variable returns errorneous path when used in DJango?
To get a current directory path you can do something like this.
import os
os.path.abspath(os.path.dirname(__file__))
This will return a normalized absolutized version of the pathname(absolute path) as well as it will normalizes the path. For example, on Unix and Mac OS X systems the path /var/www/project will work fine, but on Windows systems that is not a good path. Normalizing the path will convert forward slashes to backward slashes. Normalization also collapses redundant separators, for example the path A/foo/../B will be normalized to A/B.
Rupesh
What does __FILE__ mean in Ruby?
It is a reference to the current file name. In the file foo.rb
, __FILE__
would be interpreted as "foo.rb"
.
Edit: Ruby 1.9.2 and 1.9.3 appear to behave a little differently from what Luke Bayes said in his comment. With these files:
# test.rb
puts __FILE__
require './dir2/test.rb'
# dir2/test.rb
puts __FILE__
Running ruby test.rb
will output
test.rb
/full/path/to/dir2/test.rb
Related Topics
How to Keep Python Print from Adding Newlines or Spaces
Accessing Pandas Column Using Squared Brackets VS Using a Dot (Like an Attribute)
Generate Random Integers Between 0 and 9
Matplotlib: How to Create Axessubplot Objects, Then Add Them to a Figure Instance
How to Remove the Ansi Escape Sequences from a String in Python
How to Parse a Yaml File in Python
What Is the Use of "Assert" in Python
"For Line In..." Results in Unicodedecodeerror: 'Utf-8' Codec Can't Decode Byte
Stripping Everything But Alphanumeric Chars from a String in Python
What Is the Best Project Structure for a Python Application
Scatter Plots in Pandas/Pyplot: How to Plot by Category
How to Convert an Xml File to Nice Pandas Dataframe
How to Profile Memory Usage in Python
Extract First Item of Each Sublist
How to Convert a String with Dot and Comma into a Float in Python
Python's Time.Clock() VS. Time.Time() Accuracy