👻 Check our latest review to choose the best laptop for Machine Learning engineers and Deep learning tasks!
I recently upgrade Django from v1.3.1 to v1.4.
In my old settings.py
I have
TEMPLATE_DIRS = (
os.path.join(os.path.dirname( __file__ ), "templates").replace("", "/"),
# Put strings here, like "/home/html/django_templates" or "C:/www/django/templates".
# Always use forward slashes, even on Windows.
# Don"t forget to use absolute paths, not relative paths.
)
This will point to /Users/hobbes3/Sites/mysite/templates
, but because Django v1.4 moved the project folder to the same level as the app folders, my settings.py
file is now in /Users/hobbes3/Sites/mysite/mysite/
instead of /Users/hobbes3/Sites/mysite/
.
So actually my question is now twofold:
- How do I use
os.path
to look at a directory one level above from__file__
. In other words, I want/Users/hobbes3/Sites/mysite/mysite/settings.py
to find/Users/hobbes3/Sites/mysite/templates
using relative paths. - Should I be keeping the
template
folder (which has cross-app templates, likeadmin
,registration
, etc.) at the project/User/hobbes3/Sites/mysite
level or at/User/hobbes3/Sites/mysite/mysite
?
👻 Read also: what is the best laptop for engineering students?
Using Python"s os.path, how do I go up one directory? absolute: Questions
How to get an absolute file path in Python
3 answers
Given a path such as "mydir/myfile.txt"
, how do I find the file"s absolute path relative to the current working directory in Python? E.g. on Windows, I might end up with:
"C:/example/cwd/mydir/myfile.txt"
Answer #1
>>> import os
>>> os.path.abspath("mydir/myfile.txt")
"C:/example/cwd/mydir/myfile.txt"
Also works if it is already an absolute path:
>>> import os
>>> os.path.abspath("C:/example/cwd/mydir/myfile.txt")
"C:/example/cwd/mydir/myfile.txt"
Using Python"s os.path, how do I go up one directory? absolute: Questions
How to check if a path is absolute path or relative path in a cross-platform way with Python?
3 answers
UNIX absolute path starts with "/", whereas Windows starts with alphabet "C:" or "". Does python have a standard function to check if a path is absolute or relative?
Answer #1
os.path.isabs
returns True
if the path is absolute, False
if not. The documentation says it works in windows (I can confirm it works in Linux personally).
os.path.isabs(my_path)
Using Python"s os.path, how do I go up one directory? absolute: Questions
How to join absolute and relative urls?
3 answers
I have two urls:
url1 = "http://127.0.0.1/test1/test2/test3/test5.xml"
url2 = "../../test4/test6.xml"
How can I get an absolute url for url2?
Answer #1
You should use urlparse.urljoin :
>>> import urlparse
>>> urlparse.urljoin(url1, url2)
"http://127.0.0.1/test1/test4/test6.xml"
With Python 3 (where urlparse is renamed to urllib.parse) you could use it as follow:
>>> import urllib.parse
>>> urllib.parse.urljoin(url1, url2)
"http://127.0.0.1/test1/test4/test6.xml"
We hope this article has helped you to resolve the problem. Apart from Using Python”s os.path, how do I go up one directory?, check other absolute-related topics.
Want to excel in Python? See our review of the best Python online courses 2023. If you are interested in Data Science, check also how to learn programming in R.
By the way, this material is also available in other languages:
- Italiano Using Python”s os.path, how do I go up one directory?
- Deutsch Using Python”s os.path, how do I go up one directory?
- Français Using Python”s os.path, how do I go up one directory?
- Español Using Python”s os.path, how do I go up one directory?
- Türk Using Python”s os.path, how do I go up one directory?
- Русский Using Python”s os.path, how do I go up one directory?
- Português Using Python”s os.path, how do I go up one directory?
- Polski Using Python”s os.path, how do I go up one directory?
- Nederlandse Using Python”s os.path, how do I go up one directory?
- 中文 Using Python”s os.path, how do I go up one directory?
- 한국어 Using Python”s os.path, how do I go up one directory?
- 日本語 Using Python”s os.path, how do I go up one directory?
- हिन्दी Using Python”s os.path, how do I go up one directory?
Warsaw | 2023-03-24
Maybe there are another answers? What Using Python”s os.path, how do I go up one directory? exactly means?. Will use it in my bachelor thesis
Moscow | 2023-03-24
Maybe there are another answers? What Using Python”s os.path, how do I go up one directory? exactly means?. I am just not quite sure it is the best method
Prague | 2023-03-24
Maybe there are another answers? What Using Python”s os.path, how do I go up one directory? exactly means?. I just hope that will not emerge anymore