Python Forum
how do i write a cross-platform relative file path? - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: how do i write a cross-platform relative file path? (/thread-20647.html)



how do i write a cross-platform relative file path? - pseudo - Aug-23-2019

hi i'm current reading python crash course and it says that under unix-like platforms I have to use forward-slash like this:
with open('text_files/filename.txt') as file_object:
and under windows operating systems i have to use back-slash like this:
with open('text_files\filename.txt') as file_object:
but what if i wanted to create a multi-platform script? is there a universal way of writing file/folder paths?


RE: how do i write a cross-platform relative file path? - ichabod801 - Aug-23-2019

The second statement is false, and that code won't work anyway because the back slash will be interpreted as a special character. The truth is that you don't need to worry about it, and you can always use a forward slash.


RE: how do i write a cross-platform relative file path? - ThomasL - Aug-23-2019

Use pathlib