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!
os. path. sep is the character used by the operating system to separate pathname components.
sep. The character used by the operating system to separate pathname components.
The os.path module is always the path module suitable for the operating system Python is running on, and therefore usable for local paths. However, you can also import and use the individual modules if you want to manipulate a path that is always in one of the different formats.
Given an empty string, os. path. abspath returns the current directory, which is what you want, since the script was run from the current directory. Like the other functions in the os and os.
I would keep my fingers off os.sep and use os.path.normpath() on the result of combining the root and a filename:
filesToProcess.extend(os.path.normpath(os.path.join(root, filename))
for filename in filenames if logFilePatternMatch.match(filename))
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With