python : get list all *.txt files in a directory

This question already has an answer here:

  • How do I list all files of a directory? 31 answers
  • Find all files in a directory with extension .txt in Python 31 answers

  • you can use os , subprocess and glob library

    os library example:

    import os
    os.system("ls *.txt")
    

    this command returned all .txt file

    subprocess library example:

    my_result_command = subprocess.Popen(['ls', '-l'], stdout=log, stderr=log, shell=True)
    

    you can check my_result_command and get all file or .txt file

    glob library example:

    import glob
    glob.glob('*.txt')
    
    链接地址: http://www.djcxy.com/p/20042.html

    上一篇: Python3从特定目录列出文件

    下一篇: python:获取目录中的所有* .txt文件