Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I strip the file extension from a list full of filenames?

Tags:

I am using the following to get a list with all the files inside a directory called tokens:

import os    
accounts = next(os.walk("tokens/"))[2]

Output:

>>> print accounts
['.DS_Store', 'AmieZiel.py', 'BrookeGianunzio.py', 'FayPinkert.py', 'JoieTrevett.py', 'KaroleColinger.py', 'KatheleenCaban.py', 'LashondaRodger.py', 'LelaSchoenrock.py', 'LizetteWashko.py',  'NickoleHarteau.py']

I want to remove the extension .py from each item in this list. I managed to do it individually using os.path.splitext:

>>> strip = os.path.splitext(accounts[1])
>>> print strip
('AmieZiel', '.py')
>>> print strip[0]
AmieZiel

I'm sure I'm overdoing things, but I can't figure out a way to strip the file extension from all the items in the list with a for-loop.

What would be the proper way to do it?