On python 3.8.0 it seem to work as expected.

import os

BASE_DIR = os.path.dirname(os.path.abspath(__file__))

filepath = 'files/one.txt'
request_path = os.path.join(BASE_DIR, filepath)
print(request_path, filepath, BASE_DIR)
$ python -V
Python 3.8.0
$ python /tmp/a.py
/tmp/files/one.txt files/one.txt /tmp
Answer from shantanoo on Stack Overflow
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-os-path-join-method
Python | os.path.join() method - GeeksforGeeks
The os.path.join() method is a function in the os module that joins one or more path components intelligently.
Published: July 11, 2025
🌐
Python
docs.python.org › 3 › library › os.path.html
os.path — Common pathname manipulations
Trailing slashes are stripped from head unless it is the root (one or more slashes only). In all cases, join(head, tail) returns a path to the same location as path (but the strings may differ).
🌐
Reddit
reddit.com › r/learnpython › please help me understand os.path.join
r/learnpython on Reddit: Please help me understand os.path.join
December 12, 2021 -

Hello. I'm using windows and I want to make a new file in a directory if it doesn't exist yet.

I've read many people say that if you just use os.path.join() you don't have to worry about turning "\" into "/" manually. You don't have to worry about copying file names as paths. So i tried it because I want to do things not like an idiot, but the output of my os.path.join() still has "\". And it doesn't put a separator after "C:".

So what is the advantage of "~ just using os.path.join() like you're supposed to~"?

I understand that you can use .replace("\\","/") but what's the advantage of using os.path.join if I can just do that to a string? It seems like typing out the entire folder path in os.path.join is more work that copying and pasting it from your file window. Am i missing something?

People also ask

What does os.path.join() do in Python?
os.path.join() combines multiple path components into a single file path using the correct operating system path separator.
🌐
golinuxcloud.com
golinuxcloud.com › home › programming › python › python os.path.join() explained: syntax, examples and path handling guide
Python os.path.join() Explained: Syntax, Examples and Path Handling ...
Why should I use os.path.join instead of string concatenation?
os.path.join() automatically handles platform-specific path separators and prevents common mistakes when constructing file paths.
🌐
golinuxcloud.com
golinuxcloud.com › home › programming › python › python os.path.join() explained: syntax, examples and path handling guide
Python os.path.join() Explained: Syntax, Examples and Path Handling ...
🌐
Career Karma
careerkarma.com › blog › python › python os.path.join: a beginner’s guide
Python os.path.join: A Beginner’s Guide | Career Karma
December 1, 2023 - os.path.join() automatically adds any required forward slashes into a file path name. You may have gotten caught up in a path labyrinth when you’re working with files in Python.
🌐
Tutorialspoint
tutorialspoint.com › python › os_path_join.htm
Python os.path.join() Method
The Python os.path.join() method is used to construct a path by joining one or more path components. The method takes multiple path components (strings) and joins them together with appropriate directory separators (/ or \) depending on the
🌐
GoLinuxCloud
golinuxcloud.com › home › programming › python › python os.path.join() explained: syntax, examples and path handling guide
Python os.path.join() Explained: Syntax, Examples and Path Handling Guide | GoLinuxCloud
March 9, 2026 - The os.path.join() function always returns a string containing the combined path. The function returns a value of type str. ... The function automatically uses the correct separator based on the operating system.
🌐
Codecademy
codecademy.com › docs › python › os module › .join()
Python | os Module | .join() | Codecademy
May 29, 2025 - It takes one or more path components as arguments and joins them together with the appropriate directory separator, which varies depending on the operating system (forward slash / for Unix-based systems and backslash \ for Windows).
Find elsewhere
🌐
YouTube
youtube.com › watch
How to Join File Paths in Python Using os.path.join() | Python Path Manipulation - YouTube
In this video, we'll explore how to use the os.path.join() function in Python to efficiently and safely combine file paths. Joining paths correctly is crucia...
Published: August 22, 2024
🌐
Beautiful Soup
tedboy.github.io › python_stdlib › generated › generated › os.path.join.html
os.path.join() — Python Standard Library
Join two or more pathname components, inserting ‘/’ as needed. If any component is an absolute path, all previous path components will be discarded.
🌐
Code.mu
code.mu › en › python › manual › fs › os › path › join
The path.join method of the os module - joining paths in Python
The path.join method combines paths or their parts specified in the method parameter. The combination is performed using a separator specific to the used OS. For Windows, the separator \ is used, for Linux - /. If the last path component to be combined is empty, then the separator is placed ...
Top answer
1 of 3
113

pathlib is the more modern way since Python 3.4. The documentation for pathlib says that "For low-level path manipulation on strings, you can also use the os.path module."

It doesn't make much difference for joining paths, but other path commands are more convenient with pathlib compared to os.path. For example, to get the "stem" (filename without extension):

os.path: splitext(basename(path))[0]

pathlib: path.stem

Also, you can use the same type of syntax (commas instead of slashes) to join paths with pathlib as well:

path_2 = Path("/home", "test", "test.txt")

2 of 3
4

os.path is string-based while pathlib is object oriented

os.path operates on strings while pathlib is object-oriented (the documentation at this link speaks about Object-oriented filesystem paths). So if "/home/test/test.txt" is the absolute path (in POSIX format) to access to the file test.txt in the directory /home/test/, the instructions:

from os import path

path_1 = path.join("/home", "test", "test.txt")

(executed in a Linux system) return in path_1, the string "/home/test/test.txt" (link to os.path.join() documentation.)

On the other hand, the following commands:

from pathlib import Path

path_2 = Path("/home") / "test" / "test.txt"

return an object called path_2 which represents the file.

A function and a method available with the 2 approaches

By the previous initialization, essentially:

  • The string path_1 can be used as argument of other functions of the module os.path. For example, to check if a file exists:

    from os import path
    
    if path.exists(path_1):
        # the file exists
        do stuff
    
  • The object path_2 is an instance of the class pathlib.Path, so with this object it is possible to call all methods of the class Path.

    Furthermore, if path_2 is created on a POSIX compliant system (such as Linux), its type is PosixPath (a subclass of Path), else if path_2 is created on a Windows system, its type is WindowsPath (a subclass of the class Path).

    An example of the methods available on the object path_2 is the exists() method. The snippet of code below shows how it is used:

    from pathlib import Path
    
    if path_2.exists():
        # the file exists
        do stuff
    

Link

See this article to learn about other differences between the two ways of managing paths with Python: Paths in Python: Comparing os.path and pathlib modules

🌐
IT trip
en.ittrip.xyz › python
Detailed Explanation of Using Python’s os.path.join for File and Directory Path Concatenation | IT trip
October 22, 2024 - By the end, you will be able to handle path operations more efficiently and safely within your Python programs. ... The os.path.join function is part of Python’s standard library os module and is used to join multiple paths into a single path.
🌐
Kodeclik
kodeclik.com › python-os-join-path
Python’s os.path.join() function
October 16, 2024 - The os.path.join() function takes one or more path components denoting access to a file and concatenates them with a directory separator symbol (/).
🌐
YouTube
youtube.com › watch
Python Basics OS path Join Method - YouTube
Learn how to use the join method from the os.path module for python programming. Join concatenates two or more paths. https://github.com/Python-basics/Tutor...
Published: November 6, 2018
🌐
TechGeekBuzz
techgeekbuzz.com › home › blog › programming language › python os.path.join method a step by step guide
Python os.path.join Method a step by step Guide
The os.path.join function combines the name of two paths and return a string value of the complete pathname. With this method, we can easily combine two paths and create an absolute pathname.
🌐
Python.org
discuss.python.org › ideas
pathlib.Path.joincomponent() - Ideas - Python Discussions
January 10, 2024 - Hello, pathlib.PurePath.joinpath() uses os.path.join() to join new path components to the target path and returns a new path. An unfortunate side effect of this is that it interprets the characters os.sep and os.altsep …
🌐
YouTube
youtube.com › watch
How to Join Paths (Combine Paths) | Tutorial - 8 | OS Module | Python - YouTube
Master the art of path combination in Python with Tutorial 8 of our series! Learn how to seamlessly join paths using the os module, unlocking the potential f...
Published: January 13, 2024
🌐
Scaler
scaler.com › home › topics › python os.path.join() method
Python os.path.join() Method - Scaler Topics
January 2, 2024 - os.path.join() is a function provided by the Python os module, which works with operating system-related tasks.
🌐
Finxter
blog.finxter.com › home › learn python blog › python | join list as path
Python | Join List as Path - Be on the Right Side of Change
June 13, 2020 - The os.path.join() method takes one or more path arguments and returns a concatenation of the path arguments with the correct directory separator in your operating system. If you want to join a list of paths, you need to unpack the list into ...