list comprehension to do os.path.split_all ?

Ian Kelly ian.g.kelly at gmail.com
Thu Jul 28 23:31:43 CEST 2011


On Thu, Jul 28, 2011 at 3:15 PM, Emile van Sebille <emile at fenx.com> wrote:
> On 7/28/2011 1:18 PM gry said...
>>
>> [python 2.7] I have a (linux) pathname that I'd like to split
>> completely into a list of components, e.g.:
>>    '/home/gyoung/hacks/pathhack/foo.py'  -->   ['home', 'gyoung',
>> 'hacks', 'pathhack', 'foo.py']
>>
>> os.path.split gives me a tuple of dirname,basename, but there's no
>> os.path.split_all function.
>>
>
> Why not just split?
>
> '/home/gyoung/hacks/pathhack/foo.py'.split(os.sep)

Using os.sep doesn't make it cross-platform. On Windows:

>>> os.path.split(r'C:\windows')
('C:\\', 'windows')
>>> os.path.split(r'C:/windows')
('C:/', 'windows')
>>> r'C:\windows'.split(os.sep)
['C:', 'windows']
>>> r'C:/windows'.split(os.sep)
['C:/windows']



More information about the Python-list mailing list