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

PythonLinuxWindowsUnix

Python Problem Overview


I have written a code in python which uses / to make a particular file in a folder, if I want to use the code in windows it will not work, is there a way by which I can use the code in Windows and Linux.

In python I am using this code:

pathfile=os.path.dirname(templateFile)
rootTree.write(''+pathfile+'/output/log.txt')

When I will use my code in suppose windows machine my code will not work.

How do I use "/" (directory separator) in both Linux and Windows?

Python Solutions


Solution 1 - 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"))

Solution 2 - Python

Use:

import os
print os.sep

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

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

Solution 3 - Python

You can use os.sep:

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

Solution 4 - Python

os.path.normpath(pathname) should also be mentioned as it converts / path separators into \ separators on Windows. It also collapses redundant uplevel references... i.e., A/B and A/foo/../B and A/./B all become A/B. And if you are Windows, these all become A\B.

Solution 5 - Python

If you are fortunate enough to be running Python 3.4+, you can use pathlib:

from pathlib import Path

path = Path(dir, subdir, filename)  # returns a path of the system's path flavour

or, equivalently,

path = Path(dir) / subdir / filename

Solution 6 - Python

Some useful links that will help you:

Solution 7 - Python

Do a import os and then use os.sep

Solution 8 - Python

>>You can use "os.sep "

 import os
 pathfile=os.path.dirname(templateFile)
 directory = str(pathfile)+os.sep+'output'+os.sep+'log.txt'
 rootTree.write(directory)

Solution 9 - Python

Don't build directory and file names your self, use python's included libraries.

In this case the relevant one is os.path. Especially join which creates a new pathname from a directory and a file name or directory and split that gets the filename from a full path.

Your example would be

pathfile=os.path.dirname(templateFile)
p = os.path.join(pathfile, 'output')
p = os.path.join( p, 'log.txt')
rootTree.write(p)

Solution 10 - Python

If someone is looking for something like this ::

He/she wants to know the parent directory and then go to the sub-folders and maybe then to a specific file. If so, I use the following approach.

  1. I am using python 3.9 as of now. So in that version, we have os module for handling such tasks. So, for getting the parent directory ::

> parent_dir = os.path.pardir

  1. It's a good coding practice to not hardcode the filepath seperators (/ or \). Instead, use the operating system dependant mechanism provided by the above mentioned os module. It makes you code very much reusable for other purposes/people. It goes like this (just an example) ::

> path = os.path.pardir + os.sep + 'utils' + os.sep + 'properties.ini' > > print(f'The path to my global properties file is :: {path}')

Output :: > ..\utils\properties.ini

You can surely look at the whole documentation here : https://docs.python.org/3/library/os.html

Solution 11 - Python

I use pathlib for most things, so I like: pathlib.os.sep.

Usually pathlib is the better choice if you don't need os!

Attributions

All content for this solution is sourced from the original question on Stackoverflow.

The content on this page is licensed under the Attribution-ShareAlike 4.0 International (CC BY-SA 4.0) license.

Content TypeOriginal AuthorOriginal Content on Stackoverflow
Questionhulk007View Question on Stackoverflow
Solution 1 - PythonSerban RazvanView Answer on Stackoverflow
Solution 2 - PythonAlexander KononenkoView Answer on Stackoverflow
Solution 3 - PythonAdem ÖztaşView Answer on Stackoverflow
Solution 4 - PythonJon RosenView Answer on Stackoverflow
Solution 5 - PythonEugene YarmashView Answer on Stackoverflow
Solution 6 - PythonMarounView Answer on Stackoverflow
Solution 7 - PythonJackPointView Answer on Stackoverflow
Solution 8 - PythonP113305A009D8MView Answer on Stackoverflow
Solution 9 - PythonmmmmmmView Answer on Stackoverflow
Solution 10 - Pythonmr-possibleView Answer on Stackoverflow
Solution 11 - PythonLC117View Answer on Stackoverflow