Python module implementing common pathname manipulations
Questions tagged [os.path]
502 questions
141
votes
5 answers
Python os.path.join() on a list
I can do
>>> os.path.join("c:/","home","foo","bar","some.txt")
'c:/home\\foo\\bar\\some.txt'
But, when I do
>>> s = "c:/,home,foo,bar,some.txt".split(",")
>>> os.path.join(s)
['c:/', 'home', 'foo', 'bar', 'some.txt']
What am I missing here?

ATOzTOA
- 34,814
- 22
- 96
- 117
85
votes
5 answers
pros and cons between os.path.exists vs os.path.isdir
I'm checking to see if a directory exists, but I noticed I'm using os.path.exists instead of os.path.isdir. Both work just fine, but I'm curious as to what the advantages are for using isdir instead of exists.

user1834048
- 987
- 1
- 7
- 8
53
votes
1 answer
Pathlib vs. os.path.join in Python
When I need to define a file system path in my script, I use os.path.join to guarantee that the path will be consistent on different file systems:
from os import path
path_1 = path.join("home", "test", "test.txt")
I also know that there is Pathlib…

Amir
- 1,926
- 3
- 23
- 40
50
votes
5 answers
How can I convert os.path.getctime()?
How can I convert os.path.getctime() to the right time?
My source code is:
import os
print("My Path: "+os.getcwd())
print(os.listdir("."))
print("Root/: ",os.listdir("/"))
for items in os.listdir("."):
if os.path.isdir(items):
…

Iem-Prog
- 593
- 1
- 5
- 9
48
votes
3 answers
Python joining current directory and parent directory with os.path.join
I want to do join the current directory path and a relative directory path goal_dir somewhere up in the directory tree, so I get the absolute path to the goal_dir. This is my attempt:
import os
goal_dir = os.path.join(os.getcwd(),…

Lewistrick
- 2,649
- 6
- 31
- 42
40
votes
6 answers
Open File in Another Directory (Python)
I've always been sort of confused on the subject of directory traversal in Python, and have a situation I'm curious about: I have a file that I want to access in a directory essentially parallel to the one I'm currently in. Given this directory…

dbishop
- 910
- 2
- 8
- 13
32
votes
1 answer
Difference between os.path.dirname(os.path.abspath(__file__)) and os.path.dirname(__file__)
I am a beginner working on Django Project.
Settings.py file of a Django project contains these two lines:
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
PROJECT_ROOT = os.path.dirname(os.path.abspath(__file__))
I want to…

Rishabh Agrahari
- 3,447
- 2
- 21
- 22
31
votes
3 answers
Misunderstanding of python os.path.abspath
I have following code:
directory = r'D:\images'
for file in os.listdir(directory):
print(os.path.abspath(file))
and I want next output:
D:\images\img1.jpg
D:\images\img2.jpg and so on
But I get different…

Most Wanted
- 6,254
- 5
- 53
- 70
30
votes
4 answers
How to workaround `exist_ok` missing on Python 2.7?
On Python 2.7 os.makedirs() is missing exist_ok. This is available in Python 3 only.
I know that this is the a working work around:
try:
os.makedirs(settings.STATIC_ROOT)
except OSError as e:
if e.errno != errno.EEXIST:
raise
I…

guettli
- 25,042
- 81
- 346
- 663
21
votes
6 answers
make os.listdir() list complete paths
Consider the following piece of code:
files = sorted(os.listdir('dumps'), key=os.path.getctime)
The objective is to sort the listed files based on the creation time. However since the the os.listdir gives only the filename and not the absolute path…

Sohaib
- 4,556
- 8
- 40
- 68
17
votes
2 answers
os.path.join fails with "TypeError: object of type 'LocalPath' has no len()"
This error appeared when trying to use the 'tmpdir' in a pytest test.
TypeError: object of type 'LocalPath' has no len()

Efren
- 4,003
- 4
- 33
- 75
15
votes
2 answers
%USERPROFILE% env variable for python
I am writing a script in Python 2.7.
It needs to be able to go whoever the current users profile in Windows.
This is the variable and function I currently have:
import os
desired_paths = os.path.expanduser('HOME'\"My Documents")
I do have doubts…

Verax
- 161
- 1
- 1
- 6
15
votes
5 answers
Python idiom to get same result as calling os.path.dirname multiple times?
I find myself needing to get a parent directory of a python file in a source tree that is multiple directories up with some regularity. Having to call dirname many times is clunky.
I looked around and was surprised to not find posts on this.
The…

Tim Wilder
- 1,607
- 1
- 18
- 26
13
votes
3 answers
python os.path.getmtime() time not changing
I have a quick issue with python's os.path.getmtime() function. I have observed some weird behavior. I am working on a web app that checks periodically to see if a certain file has been modified and decides whether or not to refresh based on that.…

Mars J
- 902
- 3
- 15
- 23
13
votes
3 answers
Python os.path.relpath behavior
I have a directory bar inside a directory foo, with file foo_file.txt in directory foo and file bar_file.txt in directory bar; i.e.
computer$ ls
foo/
computer$ ls foo/
bar/ foo_file.txt
computer$ ls foo/bar/
bar_file.txt
Using the python…

jveldridge
- 1,155
- 3
- 11
- 21