I am writing a script to parse multiple log files and maintain a list of the files that have been processed. When I read the list of files to process I use os.walk
and get names similar to the following:
C:/Users/Python/Documents/Logs\ServerUI04\SystemOut_13.01.01_20.22.25.log
This is created by the following code:
filesToProcess.extend(os.path.join(root, filename) for filename in filenames if logFilePatternMatch.match(filename))
It appears that "root" used forward slashes as the separator (I am on Windows and find that more convenient) but "filename" uses backslashes so I end up with an inconsistent path for the file as it contains a mixture of forward and back slashes as separators.
I have tried setting the separator with:
os.path.sep = "/"
and
os.sep = "/"
Before the .join but it seems to have no effect. I realize that in theory I could manipulate the string but longer term I'd like my script to run on Unix as well as Windows so would prefer that it be dynamic if possible.
Am I missing something?
Update:
Based on the helpful responses below it looks like my problem was self inflicted, for convenience I had set the initial path used as root like this:
logFileFolder = ['C:/Users/Python/Documents/Logs']
When I changed it to this:
logFileFolder = ['C:\\Users\\Python\\Documents\\Logs']
Everything works and my resulting file paths all use the "\" throughout. It looks like my approach was wrong in that I was trying to get Python to change behavior rather than correcting what I was setting as a value.
Thank you!