Python Forum

Full Version: how do i write a cross-platform relative file path?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
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?
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.