Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Python - Can (or should) I change os.path.sep?

Tags:

python

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!

like image 957
Chris Avatar asked Apr 03 '13 17:04

Chris


People also ask

What does os path Sep do?

os. path. sep is the character used by the operating system to separate pathname components.

What does os Sep do in Python?

sep. The character used by the operating system to separate pathname components.

What does os path mean in Python?

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.

What is os path Abspath?

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.


1 Answers

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))    
like image 128
Anthon Avatar answered Sep 18 '22 11:09

Anthon