👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
import os A = os.path.join(os.path.dirname(__file__), "..") B = os.path.dirname(os.path.realpath(__file__)) C = os.path.abspath(os.path.dirname(__file__))
I usually just hard-wire these with the actual path. But there is a reason for these statements that determine path at runtime, and I would really like to understand the
os.path module so that I can start using it.
👻 Read also: what is the best laptop for engineering students?
what does the __file__ variable mean/do? dirname: Questions
os.path.dirname(__file__) returns empty
I want to get the path of the current directory under which a .py file is executed.
For example a simple file
D: est.py with code:
import os print os.getcwd() print os.path.basename(__file__) print os.path.abspath(__file__) print os.path.dirname(__file__)
It is weird that the output is:
D: test.py D: est.py EMPTY
I am expecting the same results from the
os.path.abspath = os.path.dirname + os.path.basename, why
os.path.abspath = os.path.dirname + os.path.basename does not hold. we rather have
os.path.dirname(filename) + os.path.basename(filename) == filename
basename() only split the passed filename into components without taking into account the current directory. If you want to also consider the current directory, you have to do so explicitly.
To get the dirname of the absolute path, use
what does the __file__ variable mean/do? dirname: Questions
What is the difference between os.path.basename() and os.path.dirname()?
What is the difference between
I already searched for answers and read some links, but didn"t understand. Can anyone give a simple explanation?
Both functions use the
os.path.split(path) function to split the pathname
path into a pair;
os.path.dirname(path) function returns the head of the path.
E.g.: The dirname of
os.path.basename(path) function returns the tail of the path.
E.g.: The basename of
Why is it string.join(list) instead of list.join(string)?
This has always confused me. It seems like this would be nicer:
my_list = ["Hello", "world"] print(my_list.join("-")) # Produce: "Hello-world"
my_list = ["Hello", "world"] print("-".join(my_list)) # Produce: "Hello-world"
Is there a specific reason it is like this?
It"s because any iterable can be joined (e.g, list, tuple, dict, set), but its contents and the "joiner" must be strings.
"_".join(["welcome", "to", "stack", "overflow"]) "_".join(("welcome", "to", "stack", "overflow"))
Using something other than strings will raise the following error:
TypeError: sequence item 0: expected str instance, int found
This was discussed in the String methods... finally thread in the Python-Dev achive, and was accepted by Guido. This thread began in Jun 1999, and
str.join was included in Python 1.6 which was released in Sep 2000 (and supported Unicode). Python 2.0 (supported
str methods including
join) was released in Oct 2000.
- There were four options proposed in this thread:
joinas a built-in function
- Guido wanted to support not only
tuples, but all sequences/iterables.
seq.reduce(str)is difficult for newcomers.
seq.join(str)introduces unexpected dependency from sequences to str/unicode.
join()as a built-in function would support only specific data types. So using a built-in namespace is not good. If
join()supports many datatypes, creating an optimized implementation would be difficult, if implemented using the
__add__method then it would ve
- The separator string (
sep) should not be omitted. Explicit is better than implicit.
Here are some additional thoughts (my own, and my friend"s):
- Unicode support was coming, but it was not final. At that time UTF-8 was the most likely about to replace UCS2/4. To calculate total buffer length of UTF-8 strings it needs to know character coding rule.
- At that time, Python had already decided on a common sequence interface rule where a user could create a sequence-like (iterable) class. But Python didn"t support extending built-in types until 2.2. At that time it was difficult to provide basic
iterableclass (which is mentioned in another comment).
Guido"s decision is recorded in a historical mail, deciding on
Funny, but it does seem right! Barry, go for it...
Guido van Rossum
join() method is in the string class, instead of the list class?
I agree it looks funny.
Historical note. When I first learned Python, I expected join to be a method of a list, which would take the delimiter as an argument. Lots of people feel the same way, and there‚Äôs a story behind the join method. Prior to Python 1.6, strings didn‚Äôt have all these useful methods. There was a separate string module which contained all the string functions; each function took a string as its first argument. The functions were deemed important enough to put onto the strings themselves, which made sense for functions like lower, upper, and split. But many hard-core Python programmers objected to the new join method, arguing that it should be a method of the list instead, or that it shouldn‚Äôt move at all but simply stay a part of the old string module (which still has lots of useful stuff in it). I use the new join method exclusively, but you will see code written either way, and if it really bothers you, you can use the old string.join function instead.
--- Mark Pilgrim, Dive into Python
We hope this article has helped you to resolve the problem. Apart from what does the __file__ variable mean/do?, check other dirname-related topics.
By the way, this material is also available in other languages:
- Italiano what does the __file__ variable mean/do?
- Deutsch what does the __file__ variable mean/do?
- Français what does the __file__ variable mean/do?
- Español what does the __file__ variable mean/do?
- Türk what does the __file__ variable mean/do?
- Русский what does the __file__ variable mean/do?
- Português what does the __file__ variable mean/do?
- Polski what does the __file__ variable mean/do?
- Nederlandse what does the __file__ variable mean/do?
- 中文 what does the __file__ variable mean/do?
- 한국어 what does the __file__ variable mean/do?
- 日本語 what does the __file__ variable mean/do?
- हिन्दी what does the __file__ variable mean/do?
Tallinn | 2022-11-30
realpath is always a bit confusing 😭 what does the __file__ variable mean/do? is not the only problem I encountered. I just hope that will not emerge anymore
Berlin | 2022-11-30
Maybe there are another answers? What what does the __file__ variable mean/do? exactly means?. Checked yesterday, it works!
Paris | 2022-11-30
mean is always a bit confusing 😭 what does the __file__ variable mean/do? is not the only problem I encountered. Will get back tomorrow with feedback