Python Forum
how do i write a cross-platform relative file path?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
how do i write a cross-platform relative file path?
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.
Craig "Ichabod" O'Brien -
I wish you happiness.
Recommended Tutorials: BBCode, functions, classes, text adventures
Use pathlib

Possibly Related Threads…
Thread Author Replies Views Last Post
  rows from sql query need to write to a file as columns sjcsvatt 6 234 Oct-09-2021, 12:45 AM
Last Post: snippsat
  Can't write to file Mark17 2 206 Sep-24-2021, 03:45 PM
Last Post: Mark17
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 258 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  How to write in text file - indented block Joni_Engr 3 459 Aug-09-2021, 06:30 PM
Last Post: deanhystad
  Read and write active Excel file euras 4 625 Jun-29-2021, 11:16 PM
Last Post: Pedroski55
  Read file, reformat and write new file bryanmartin113 1 622 Jun-08-2021, 09:27 PM
Last Post: Larz60+
  How to reference the relative directory when creating a photoimage kenwatts275 3 796 May-18-2021, 07:22 PM
Last Post: menator01
  Subprocess.Popen() not working when reading file path from csv file herwin 13 2,163 May-07-2021, 03:26 PM
Last Post: herwin
  write mariadb table rows query to each file? shams 1 730 Feb-02-2021, 04:10 PM
Last Post: buran
  Read/Write binary file deanhystad 3 1,033 Feb-01-2021, 10:29 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020