I am trying to identify all files with certain names in a folder. I am using standard code to do that looking like this:
for paths, subdirs, files in os.walk(start_dir, topdown=True):
for file in files:
print(os.path.join(paths, file))
My problem is about the output of this code in windows machine, basically dynamic parts of the path have wrong slash sign:
D:/JAJA/Projects/DAF/AIM/WEBAPP/trunk/src/main/java/ie/gov/agriculture/aim\aes\AesSheetNumberEntity.java
D:/JAJA/Projects/DAF/AIM/WEBAPP/trunk/src/main/java/ie/gov/agriculture/aim\aes\DocumentReceivedDetailEntity.java
D:/JAJA/Projects/DAF/AIM/WEBAPP/trunk/src/main/java/ie/gov/agriculture/aim\aes\DocumentReceivedEntity.java
D:/JAJA/Projects/DAF/AIM/WEBAPP/trunk/src/main/java/ie/gov/agriculture/aim\aes\DocumentTypeEntity.java
start folder which was given is:
D:/JAJA/Projects/DAF/AIM/WEBAPP/trunk/src/main/java/ie/gov/agriculture/aim
and the folder separator is unix one: "/"
while all subsequent subfolders found by os.walk function have windows slash instead: "\"
So at the end I have invalid path which cannot be used straight away. Is this a bug in python os library or what actually?
Currently I can easily replace wrong separator with the right one but I am wondering if it is the only way?
There is no actual problem here. Windows supports two path separators; the forward and backward slashes are both valid and supported, even when mixed. One is the
os.sep(\), and the other theos.altsepcharacter (/).os.path.join()useros.septo join paths, but won't replaceos.altsepin the input paths.os.walk()just usesos.path.join()to build the first element of each(path, files, directories)tuple it generatesIf this bothers you, normalise your paths, using the
os.path.normpath()function:So normalise the path passed to
os.walk():or normalise the paths generated in the loop:
or normalise the input string: