我正在使用下面的代码来获取名为tokens
的目录中的所有文件列表:
import os
accounts = next(os.walk("tokens/"))[2]
输出:
>>> print accounts
['.DS_Store', 'AmieZiel.py', 'BrookeGianunzio.py', 'FayPinkert.py', 'JoieTrevett.py', 'KaroleColinger.py', 'KatheleenCaban.py', 'LashondaRodger.py', 'LelaSchoenrock.py', 'LizetteWashko.py', 'NickoleHarteau.py']
我希望能够从这个列表中的每个项中删除扩展名
.py
。我已经使用os.path.splitext
单独完成了这个操作:>>> strip = os.path.splitext(accounts[1])
>>> print strip
('AmieZiel', '.py')
>>> print strip[0]
AmieZiel
我确定我做得过头了,但我无法想出一种使用for循环从列表中剥离所有项目的文件扩展名的方法。
应该如何正确地做呢?