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 the os.altsep
character (/
).
os.path.join()
user os.sep
to join paths, but won't replace os.altsep
in the input paths. os.walk()
just uses os.path.join()
to build the first element of each (path, files, directories)
tuple it generates
If this bothers you, normalise your paths, using the os.path.normpath()
function:
On Windows, it converts forward slashes to backward slashes.
So normalise the path passed to os.walk()
:
for paths, subdirs, files in os.walk(os.path.normpath(start_dir), topdown=True):
for file in files:
full_path = os.path.join(paths, file)
print(full_path)
or normalise the paths generated in the loop:
for paths, subdirs, files in os.walk(start_dir, topdown=True):
for file in files:
full_path = os.path.join(paths, file)
normalised = os.path.normpath(full_path)
print(normalised)
or normalise the input string: