Maison  >  Questions et réponses  >  le corps du texte

python - 如何在目录下按照一定顺序提取出指定的文件名?

dio.log
dio.log.1
dio.log.2
dio.log.3
dio.log.4
dio.log.5
GTKBrowserLog_sleboot.log
GTKBrowserLog_sleoper.log
GTKBrowserLog_tvmticket.log
HoterStatus.log
logfiledir.txt
mode.log
mode.log.1
mode.log.2
mode.log.3
mode.log.4
mode.log.5
ParamDownload.log
sleboot.log
slebooten.log
slecomm.log
slecomm.log.1
slecommpack.log
slecommpack.log.1
slecommstatus.log
sleopercn.log
sleOperMain.log
TpuUnit1.log
TpuUnit1.log.1
TpuUnit2.log
tvmbill.log
tvmbill.log.1
tvmbill.log.2
tvmbill.log.3
tvmbill.log.4
tvmbill.log.5
tvmbillBiz.log
tvmbillBiz.log.1
tvmbillBiz.log.2
tvmbillchger.log
tvmbillchger.log.1
tvmbillchger.log.2
tvmbillchger.log.3
tvmbillchgerBiz.log
tvmcoin.log
tvmcoin.log.1
tvmcoin.log.2
tvmcoin.log.3
tvmcoin.log.4
tvmcoin.log.5
tvmcoinen.log
tvmcore.log
tvmcore.log.1
tvmcore.log.2
tvmissue.log
tvmissue.log.1
tvmissueen.log
TvmLed.log
tvmticket.log
tvmticket.log.1
tvmticket.log.2

以上是一个目录下的文件名,如何能将tvmticket.log、tvmticket.log.1、tvmticket.log.2 这3个文件的文件名提取出来存放在列表中,并且存放的顺序是['tvmticket.log.2','tvmticket.log.1','tvmticket.log']
请教各位大神如何能实现呢?

伊谢尔伦伊谢尔伦2740 Il y a quelques jours1924

répondre à tous(1)je répondrai

  • PHPz

    PHPz2017-04-18 10:30:09

    Les noms des fichiers que vous devez filtrer sont-ils fixes ? Si cela est corrigé, séparez le nom du fichier et l'extension, puis faites correspondre

    file_lst = []
    for path, dir, files in os.walk('./'):
        file_lst += files
    file_count = len(file_lst) * 1.0
    for key, lst in groupby(file_lst, key=lambda x: os.path.splitext(x)[0]):
        if cmp(key, 'tvmticket') == 0:
          pass

    répondre
    0
  • Annulerrépondre