To be even more pedantic, the most python doc consistent answer would be:

mypath = os.path.join('c:', os.sep, 'sourcedir')

Since you also need os.sep for the posix root path:

mypath = os.path.join(os.sep, 'usr', 'lib')
Answer from AndreasT on Stack Overflow
🌐
Python
docs.python.org › 3 › library › os.path.html
os.path — Common pathname manipulations — Python 3.15.2 documentation
Note that since there is a current directory for each drive, os.path.join("c:", "foo") represents a path relative to the current directory on drive C: (c:foo), not c:\foo.
🌐
GeeksforGeeks
geeksforgeeks.org › python › python-os-path-join-method
Python | os.path.join() method - GeeksforGeeks
Explanation: In this example, the `os.path.join()` method is employed to generate the full path for each file in the current working directory. The complete paths are then printed, allowing for a comprehensive listing of all files in the directory.
Published: July 11, 2025
Discussions

Please help me understand os.path.join
os.path.join has different results based on the operating system the code is running from. If your code wants to support linux and windows, you'll need to add '\' for windows, and '/' for linux. This does this automatically for you. More on reddit.com
🌐 r/learnpython
4
3
December 12, 2021
Python os.path.join on Windows - Stack Overflow
If this os.path.join can't be used this way, then I am confused as to its purpose. According to the pages suggested by Stack Overflow, slashes should not be used in join—that is correct, I assume? ... Save this answer. ... Show activity on this post. To be even more pedantic, the most python doc ... More on stackoverflow.com
🌐 stackoverflow.com
python - Why doesn't os.path.join() join absolute paths? - Stack Overflow
The below code will not join, when debugged the command does not store the whole path but just the last entry. os.path.join('/home/build/test/sandboxes/', todaystr, '/new_sandbox/') When I test th... More on stackoverflow.com
🌐 stackoverflow.com
pathlib.Path vs. os.path.join in Python - Stack Overflow
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. More on stackoverflow.com
🌐 stackoverflow.com
People also ask

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 ...
Can os.path.join accept multiple arguments?
Yes, os.path.join() accepts multiple path segments and combines them in order to produce a valid path.
🌐
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.
🌐
Codecademy
codecademy.com › docs › python › os module › .join()
Python | os Module | .join() | Codecademy
May 29, 2025 - Learn the basics of Python 3.13, one of the most powerful, versatile, and in-demand programming languages today. ... *paths: Additional path components that will be joined to the base path with the appropriate separator. ... The method returns a string representing the combined path. This example demonstrates how to join directory and file names to create a complete file path: ... In this example, os.path.join() combines the directory name "user_data" with the filename "config.txt" using the appropriate separator for the current operating system.
🌐
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?

🌐
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 solves this problem by combining path segments using the correct separator for the current operating system, making path construction both safe and cross-platform.
Find elsewhere
🌐
Tutorialspoint
tutorialspoint.com › python › os_path_join.htm
Python os.path.join() Method
In the following example, we are joining two path components, "/path/to" and "file.txt", to form a path using the join() method − · import os path1 = "/path/to" path2 = "file.txt" joined_path = os.path.join(path1, path2) print(joined_path)
🌐
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.
🌐
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 (/).
🌐
OpenPython
openpython.org › home › articles › python os.path guide: join, check, and split file paths
Python os.path Guide: Join, Check, and Split File Paths | OpenPython
March 25, 2026 - Learn Python os.path with practical examples: join() paths, check if files exist, split filenames from extensions, and know when to switch to pathlib instead.
🌐
How to Use Linux
howtouselinux.com › home › understanding os.path.join in python
understanding os.path.join in Python - howtouselinux
October 9, 2025 - The os.path.join() method can be used to join two paths together into a single string.
🌐
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.
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

🌐
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 ...
🌐
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.
🌐
Python Academy
python-academy.org › function reference › os.path.join
os.path.join — Python Function Reference
A detailed reference of Python functions with examples. Find out how os.path.join works in Python. Join path parts with the separator of the current operating system.