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!
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 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.
Syntax: os.path.join(path, *paths)
Description: Join one or more path components in path
and *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)
Examples: Let’s dive into a minimal example how you can join the path stored in a Python list.
import os p = ['c:', 'user', 'path', 'file.py'] print(os.path.join(*p))
The output is the joined path:
'c:user\path\file.py'
The code performs the following steps:
- Import the
os
module. - 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:
os.path.join('', *p)
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!
Coders get paid six figures and more because they can solve problems more effectively using machine intelligence and automation.
To become more successful in coding, solve more real problems for real people. 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?
You build high-value coding skills by working on practical coding projects!
Do you want to stop learning with toy projects and focus on practical code projects that earn you money and solve real problems for people?
🚀 If your answer is YES!, consider becoming 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.
If you just want to learn about the freelancing opportunity, feel free to watch my free webinar “How to Build Your High-Income Skill Python” and learn how I grew my coding business online and how you can, too—from the comfort of your own home.