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?
Answers:
Do a import os
and then use os.sep
Some useful links that will help you:
You can use os.sep:
>>> import os
>>> os.sep
'/'
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')
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"))
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)
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
.
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
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)
I use pathlib
for most things, so I like: pathlib.os.sep
.
Usually pathlib
is the better choice if you don’t need os
!
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.
- 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
- 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
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?
Do a import os
and then use os.sep
Some useful links that will help you:
You can use os.sep:
>>> import os
>>> os.sep
'/'
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')
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"))
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)
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
.
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
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)
I use pathlib
for most things, so I like: pathlib.os.sep
.
Usually pathlib
is the better choice if you don’t need os
!
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.
- 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
- 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