Let's say I have a class which represents a directory (simplified example of course):
import os
class Dir:
def __init__(self, path):
self.path = os.path.normcase(path)
To make things easier to implement internally, I am calling os.path.normcase
on the path
argument before I save it into an attribute. This works great, but it lowercases the path:
>>> import os
>>> os.path.normcase(r'C:\Python34\Lib')
'c:\\python34\\lib'
>>>
I would like a way to turn the path back into its properly capitalized form of C:\Python34\Lib
. I plan to do this inside the __repr__
method so that I can get nice outputs such as:
>>> my_dir
Dir(r'C:\Python34\Lib')
>>>
when I am in the interactive interpreter. Is there anything like this in the standard library?
Note: I am not referring to the string that the user supplied as the path
argument. If a user does:
my_dir = Dir('c:\PYTHON34\lib')
I still want Dir('C:\Python34\Lib')
to be printed in the interpreter because that is the proper capitalization. Basically, I want the outputed paths to be the same as they are in the file explorer.