I use the following to get a list with all the files inside a directory called tokens :
import os accounts = next(os.walk("tokens/"))[2]
Conclusion:
>>> 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 .py extension from each item in this list. I managed to do this individually using os.path.splitext :
>>> strip = os.path.splitext(accounts[1]) >>> print strip ('AmieZiel', '.py') >>> print strip[0] AmieZiel
I'm sure I overdid it, but I can't figure out how to remove the file extension from all the items in the list using a for loop.
What would be the right way to do this?
source share