How to use "/" (directory separator) in both Linux and Windows in Python? How to use "/" (directory separator) in both Linux and Windows in Python? python python

How to use "/" (directory separator) in both Linux and Windows in Python?


Use os.path.join().Example: os.path.join(pathfile,"output","log.txt").

In your code that would be: rootTree.write(os.path.join(pathfile,"output","log.txt"))


Use:

import osprint os.sep

to see how separator looks on a current OS.
In your code you can use:

import ospath = os.path.join('folder_name', 'file_name')


You can use os.sep:

>>> import os>>> os.sep'/'