I have a function that receives full file path as a string and the goal is to return just the file path without the filename. The goal also is to handle all file type such as Windows, Unix etc. I had to replace \ with / which works fine for Windows file paths but seems unnecessary for Unix paths.
def get_path(file):
file = file.replace('\\', '/')
path = os.path.dirname(file)
return path
Is there any way to extract the path without replacing the backward slash? Here's are two example input path strings
Windows: 'c:\\Program Files\\user\\file1.txt'
Unix: 'file/path/name.txt'
I have searched through stack but couldn't find any answer that works for me.