Convert Windows Path To Linux In Python
Last updated June 21, 2023 by Jarvis Silva
As a developer, it’s common to work on multiple operating systems, However, each operating system has its own file path format, which can sometimes cause problems.
So it is important to convert them to their operating system suitable path, In this article you are exactly going to see how to convert windows path linux path in python.
Above are the examples of how windows and linux path as you can see it uses different backslashes so let’s see how to convert them.
Converting Windows Path To Linux In Python Using Pathlib Module
from pathlib import Path windows_path = r'C:\Users\JohnDoe\Documents\example.txt' linux_path = Path(windows_path).as_posix() print(linux_path)
As you can see it just 3 lines of code because we used pathlib module to convert windows path to linux, After running this program you will see it printing the path in linux format like below
As you can see we successfully converted windows path to linux path using python, I hope you found this tutorial helpful and useful do share it with someone who might find it helpful.
Thanks for reading, Have a nice day 🙂
Free courses and programming resourcesJoin Telgram