目录中有多个文件也包含数字和非数字。让我们说abc1710.csv,xyz1709.txt,abc1708.txt,abc.txt,xyz.csv。 我想从fileName中仅提取最新的YYMM。
FileNames = (next(os.walk('C:\\Python34\\PyScript'))[2])
def check_file_name(f):
try:
digits = f[-4:]
if len(digits) != 4:
return False
int(f[-4:])
except:
return False
return True
# first filter out bad file names:
good_filenames = [x for x in FileNames if check_file_name(x)]
# now run the code on "good names" only:
fileName=(max(good_filenames))
value=(fileName[-4:])
result = re.sub(r'[a-z]+', '', fileName)
print(result)
答案 0 :(得分:0)
函数check_file_name
假定扩展已经被修剪,但是你给它整个文件名。应该通过
good_filenames = [x for x in FileNames if check_file_name(x[:-4])]
此外,如果您想获得最大的数字,您还必须对数字进行比较,而不是文件名:
fileName=max(good_filenames, key=lambda x: int(x[-8:-4]))
未来的建议:
(max(good_filenames))
)