【发布时间】:2015-01-03 00:51:16
【问题描述】:
我正在使用以下内容获取一个列表,其中包含一个名为 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 循环从列表中的所有项目中删除文件扩展名。
正确的做法是什么?
【问题讨论】:
标签: python list path file-extension