Python | Join List as Path

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)

Python | Join List as Path

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:

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!

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.

Join the free webinar now!