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

Question:

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?

Asked By: hulk007

||

Answers:

Do a import os and then use os.sep

Answered By: JackPoint

Some useful links that will help you:

Answered By: Maroun

You can use os.sep:

>>> import os
>>> os.sep
'/'
Answered By: Adem ÖztaƟ

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')
Answered By: Alexander Kononenko

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

Answered By: Serban Razvan

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)
Answered By: mmmmmm

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 AB.

Answered By: Jon Rosen

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
Answered By: Eugene Yarmash

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)
Answered By: P113305A009D8M

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

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

Answered By: LC117

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 than 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 the 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 file path separators (/ or ). Instead, use the operating system dependant mechanism provided by the above-mentioned os module. It makes your 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:

..utilsproperties.ini

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

Answered By: mr-possible
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.