Python Os.path.dirname Returns Unexpected Path When Changing Directory
Currently I do not understand, why pythons os.path.dirname behave like it does. Let's assume I have the following script: # Not part of the script, just for the current sample __fi
Solution 1:
Look into os.path.normpath
Normalize a pathname by collapsing redundant separators and up-level references so that A//B, A/B/, A/./B and A/foo/../B all become A/B. This string manipulation may change the meaning of a path that contains symbolic links. On Windows, it converts forward slashes to backward slashes.
The reason os.path.dirname
works the way it does is because it's not very smart - it even work for URLs!
os.path.dirname("http://www.google.com/test") # outputs http://www.google.com
It simply chops off anything after the last slash. It doesn't look at anything before the last slash, so it doesn't care if you have /../
in there somewhere.
Solution 2:
os.path.normpath()
will return a normalized path, with all references to the current or parent directory removed or replaced appropriately.
Post a Comment for "Python Os.path.dirname Returns Unexpected Path When Changing Directory"