Python Build Full Path

Aug 28, 2020
import osmain_dir = '/code/'sub_dir = 'project/sub/'filename = 'README.md'# /code/project/sub/README.mdos.path.join(main_dir, sub_dir, filename)

NOTE: Becareful if sub_dir is /project/sub/, the output will be /project/sub/README.md instead.

You could use pathlib as well, which comes with useful file operations

from pathlib import Pathmain_dir = '/code/'sub_dir = 'project/sub/'filename = 'README.md'path = Path(main_dir, sub_dir, filename)path.is_file()

References:

❤️ Is this article helpful?

Buy me a coffee ☕ or support my work via PayPal to keep this space 🖖 and ad-free.

Do send some 💖 to @d_luaz or share this article.

✨ By Desmond Lua

A dream boy who enjoys making apps, travelling and making youtube videos. Follow me on @d_luaz

👶 Apps I built

Travelopy - discover travel places in Malaysia, Singapore, Taiwan, Japan.