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

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?

Answers:

Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.

Method 1

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

Method 2

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

Method 3

You can use os.sep:

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

Method 4

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.

Method 5

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

Method 6

Some useful links that will help you:

Method 7

Do a import os and then use os.sep

Method 8

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)

Method 9

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)

Method 10

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 ::

..utilsproperties.ini

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

Method 11

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

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


All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0

0 0 votes
Article Rating
Subscribe
Notify of
guest

0 Comments
Oldest
Newest Most Voted
Inline Feedbacks
View all comments
0
Would love your thoughts, please comment.x
()
x