I'm having different behavior for Windows and Linux in the following case.
import os
path = '..\\file.hdf'
norm_path = os.path.normpath(path)
splitted_path = os.path.split(norm_path)
print(splitted_path)
On Windows I get ('', 'file.hdf')
On Linux I get ('', '..\\file.hdf')
Is there a better/specific way to use the os.path for this?
Ok, it's easily fixed with norm_path.split('\\'), but that's not dynamic at all.
On Linux, paths are separated with a forward slash. If you want a platform-independent approach, I suggest using os.sep instead of backslash:
import os
path = '..' + os.sep + 'file.hdf'
norm_path = os.path.normpath(path)
split_path = os.path.split(norm_path)
print(split_path)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With