Do you want to join a list of strings to a path in your specific operating system? Remembering the correct path separator can be a real pain. Fortunately, the
os.path.join() method comes to the rescue!
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 the argument list. For example,
os.path.join(*lst) would join the list
['c:', 'your', 'directory'] into the path string
'c://your/directory' in a Windows environment.
Description: Join one or more path components in
*paths. Concatenates path components using the directory separator string stored in
os.sep. If a component is an absolute path such as
'c:\user\path' or a drive letter such as
'c:', all previous path components are thrown away and joining continues from the absolute path component. (improved from docs)
import os p = ['c:', 'user', 'path', 'file.py'] print(os.path.join(*p))
The output is the joined path:
The code performs the following steps:
- Import the
- Create a list of path components stored as strings. The string
'c:'is denoted the drive letter.
- Create a string by joining all path components using the os.path.join(…) method. You unpack all strings in the list using the asterisk operator. For more information about the asterisk operator, check out my detailed tutorial on the Finxter blog.
- Print the result to the shell.
Here’s what happens if you use two drive letters in your path components:
import os print(os.path.join('c:', 'user', 'd:', 'path', 'file.py')) # d:path\file.py
The second drive letter
d: overwrites the first drive letter and all previous components (with respect to
d:) are thrown away!
In case, the list can be empty, unpacking the empty list into the
os.path.join() function will throw an error:
import os p =  print(os.path.join(*p)) ''' Traceback (most recent call last): File "C:\Users\xcent\Desktop\code.py", line 13, in <module> print(os.path.join(*p)) TypeError: join() missing 1 required positional argument: 'path' '''
You can fix this by using the following trick:
Even if the list
p is empty, it’ll still return an empty path avoiding the error message. This trick is useful in many other scenarios, so it pays to have it seen once.
Asterisk Operator Explainer Video (Unpacking Lists)
Where to Go From Here?
Enough theory, let’s get some practice!
To become successful in coding, you need to get out there and solve real problems for real people. That’s how you can become a six-figure earner easily. And that’s how you polish the skills you really need in practice. After all, what’s the use of learning theory that nobody ever needs?
Practice projects is how you sharpen your saw in coding!
Do you want to become a code master by focusing on practical code projects that actually earn you money and solve problems for people?
Then become a Python freelance developer! It’s the best way of approaching the task of improving your Python skills—even if you are a complete beginner.
Join my free webinar “How to Build Your High-Income Skill Python” and watch how I grew my coding business online and how you can, too—from the comfort of your own home.
While working as a researcher in distributed systems, Dr. Christian Mayer found his love for teaching computer science students.
To help students reach higher levels of Python success, he founded the programming education website Finxter.com. He’s author of the popular programming book Python One-Liners (NoStarch 2020), coauthor of the Coffee Break Python series of self-published books, computer science enthusiast, freelancer, and owner of one of the top 10 largest Python blogs worldwide.
His passions are writing, reading, and coding. But his greatest passion is to serve aspiring coders through Finxter and help them to boost their skills. You can join his free email academy here.