Kodeclik Blog


Python’s os.path.join() function

The os.path.join() function in Python is a simple way to concatenate multiple strings into a full filepath. os.path is a module and join() is a function within that module.
Consider for instance that we have a repl and are writing the following simple program in it:
The output is (your specific output can vary):
This program prints the current working directory as: /home/runner/MedicalOrneryGame and if your program is called, say, “main.py”, then the full pathname is “/home/runner/MedicalOrneryGame/main.py”.
os.path.join() can be viewed as a string concatenation function specifically aimed at creating file paths. Below is a program that creates a new filepath within the same directory as the above program:
The output will be:
Note that Python has taken the current working directory (i.e., /home/runner/MedicalOrneryGame), the filename (which is “newfile.py”) and concatenated them with a natural directory separator (i.e., “/”) in between. In other words, we did not have to specify the directory separator explicitly.
Now that we have the new filepath we can apply other file operations on it, e.g., creating this file, modifying, and later deleting this file. Thus, os.path.join() is a convenient way to create directory paths.
os.path.join() also allows multiple arguments, like so:
In this program we are using os.path.join with multiple arguments which will all be concatenated using the file separator symbol. The output is:
If you added end slashes to some of these file path components, like so:
The output is:
i.e., note that Python knows to discard them intelligently. However if you give multiple ending slashes Python does not ignore them. Thus in the program:
The output is:
If you use slashes at the beginning of the string, however, Python assumes that this is where the filepath begins and discards anything that came before it. Thus:
returns:
In summary, os.path.join() is a very convenient tool to construct filepaths. Why do we need to use it? Two reasons: (1) It takes care of adding the file separator itself. So even if you forget, Python doesn’t. (2) Even though you can achieve the result by string concatenation, using os.join.path() conveys to others reading your code that you are constructing a filepath (unlike say a name or an address).
In this blogpost, we have seen how Python’s os.path.join() function can help create directory paths from within your Python program. If you liked this, checkout our blogpost on the Python os.mkdir() function which as you can surmise helps create directories! Also checkout os.listdir() which lists the contents of a directory and os.remove() which removes files from within Python!
For more Python content, checkout the math.ceil() and math.floor() functions! Also learn about the math domain error in Python and how to fix it!
Interested in more things Python? Checkout our post on Python queues. Also see our blogpost on Python's enumerate() capability. Also if you like Python+math content, see our blogpost on Magic Squares. Finally, master the Python print function!
Want to learn Python with us? Sign up for 1:1 or small group classes.

Join our mailing list

Subscribe to get updates about our classes, camps, coupons, and more.
  • ABOUT

Copyright @ Kodeclik 2023. All rights reserved.