Bottom Page

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
ndc85430 and scidam like this post

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Does python guarantees that file.write() works without file.close() ? kryptomatrix 6 145 Oct-18-2019, 11:44 PM
Last Post: snippsat
  pyautogui screenshotting region relative to image found on screen Bmart6969 3 178 Oct-05-2019, 06:20 PM
Last Post: Bmart6969
  How do I read the HTML files in a directory and write the content into a CSV file? glittergirl 1 108 Sep-23-2019, 11:01 AM
Last Post: Larz60+
  How do I write class objects to a file in binary mode? Exsul1 7 399 Sep-14-2019, 09:33 PM
Last Post: snippsat
  Generate String Only Unique to That Platform firesh 2 127 Sep-10-2019, 09:36 PM
Last Post: firesh
  write to txt file one line antmar904 3 240 Aug-16-2019, 02:40 PM
Last Post: Axel_Erfurt
  read text file and write into html with correct link jacklee26 4 239 Aug-02-2019, 05:48 AM
Last Post: jacklee26
  File Path not recognised starzar 3 226 Jul-27-2019, 05:23 AM
Last Post: starzar
  Best way of taking a date prefix from a line and forming a file path from it? Skaperen 5 314 Jul-22-2019, 11:50 PM
Last Post: Skaperen
  Writing the Path to a file properly dcw9996 2 256 Jul-01-2019, 11:26 PM
Last Post: dcw9996

Forum Jump:

Users browsing this thread: 1 Guest(s)