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
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

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
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
correct way of using os.path.join() in python - Stack Overflow
I came across this python function os.path.join(). More on stackoverflow.com
๐ŸŒ stackoverflow.com
python - How to join paths nice way? - Stack Overflow
You could use the f-string in python (see documentation). ... Be aware that you need an extra slash "/" between your file location variables (BASE_DIR and filepath) to complete the full file location properly. Hope this helps! ... It's not, though if you are going to hard-code the / in filepath, you may as well hard-code it every where. os.path.join ... More on stackoverflow.com
๐ŸŒ stackoverflow.com
๐ŸŒ
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?

๐ŸŒ
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.
๐ŸŒ
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.
Find elsewhere
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

๐ŸŒ
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
๐ŸŒ
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)
๐ŸŒ
Samy
samy.blog โ€บ rtfm
RTFM, and Python's os.path.join | Samy's blog
file_path = os.path.join("uploads", "static", file_name)
๐ŸŒ
Jython
jython.org โ€บ jython-old-sites โ€บ docs โ€บ library โ€บ os.path.html
10.1. os.path โ€” Common pathname manipulations โ€” Jython v2.5.2 documentation
The os.path module is always the path module suitable for the operating system Python is running on, and therefore usable for local paths. However, you can also import and use the individual modules if you want to manipulate a path that is always in one of the different formats. They all have the same interface: ... Return a normalized absolutized version of the pathname path. On most platforms, this is equivalent to normpath(join(os.getcwd(), path)).
๐ŸŒ
Read the Docs
python.readthedocs.io โ€บ fr โ€บ latest โ€บ library โ€บ os.path.html
11.2. os.path โ€” Common pathname manipulations โ€” documentation Python 3.7.0a0
Join one or more path components intelligently. The return value is the concatenation of path and any members of *paths with exactly one directory separator (os.sep) following each non-empty part except the last, meaning that the result will only end in a separator if the last part is empty.
๐ŸŒ
Medium
medium.com โ€บ codetodeploy โ€บ 9-python-features-i-ignored-for-years-until-they-cut-my-work-time-in-half-20516e240fb0
9 Python Features I Ignored for Years โ€” Until They Cut My Work Time in Half | by KhadijahDevLogs | CodeToDeploy | Medium
April 11, 2026 - Started trusting Python more. I avoided this module for years because it โ€œlooked complicated.โ€ ยท Big mistake. import itertools nums = [1, 2, 3] pairs = list(itertools.combinations(nums, 2)) print(pairs) Output: [(1, 2), (1, 3), (2, 3)] No nested loops. No headaches. Pro tip: If youโ€™re writing nested loops, thereโ€™s a high chance itertools already solved it. I used to juggle os and os.path like a circus act.
๐ŸŒ
Reddit
reddit.com โ€บ r โ€บ golang
The Go Programming Language
November 11, 2009 - I'm very new to Go, and right now I'm rewriting one of my Python projects (https://codeberg.org/theesplatt/Wallflower) in Go, but I'm having some troubles with project structure.
๐ŸŒ
PyPI
pypi.org โ€บ project โ€บ python-dotenv
python-dotenv ยท PyPI
Require Python 3.5 or a later version. Python 2 and 3.4 are no longer supported. (#341 by @bbc2). The dotenv_path argument of set_key and unset_key now has a type of Union[str, os.PathLike] instead of just os.PathLike (#347 by @bbc2).
๐ŸŒ
CSE CGI Server
cgi.cse.unsw.edu.au โ€บ ~cs2041 โ€บ 26T1 โ€บ assignments โ€บ ass2 โ€บ index.html
COMP(2041|9044) 26T1 โ€” Assignment 2: Sharpie
April 14, 2026 - The '#!' line in the example generated code assumes /usr/bin/python3 is the pathname of the Python interpreter, which it is on CSE systems and many other places.