[Python-ideas] PEP 428 - object-oriented filesystem paths

Eric Snow ericsnowcurrently at gmail.com
Sat Oct 6 07:16:55 CEST 2012


On Fri, Oct 5, 2012 at 5:48 PM, Antoine Pitrou <solipsis at pitrou.net> wrote:
> On Fri, 05 Oct 2012 14:38:57 -0700
> Ethan Furman <ethan at stoneleaf.us> wrote:
>>
>> Say I have a .dbf table as PureNTPath('c:\orders\12345\abc67890.dbf'),
>> and I export it to .csv in the same folder; how would I transform the
>> above PureNTPath's ext from 'dbf' to 'csv'?
>
> Something like:
>
>>>> p = PureNTPath('c:/orders/12345/abc67890.dbf')
>>>> p.parent()[p.name.split('.')[0] + '.csv']
> PureNTPath('c:\\orders\\12345\\abc67890.csv')
>
> Any suggestion to ease this use case a bit?

Each namedtuple has a _replace() method that's is used to generate a
new instance with one or more attributes changed.  We could do
something similar here:

>>> p = PureNTPath('c:/orders/12345/abc67890.dbf')
>>> p.replace(ext='.csv')
PureNTPath('c:\\orders\\12345\\abc67890.csv')

-eric



More information about the Python-ideas mailing list