https://stackoverflow.com/questions/3964681/find-all-files-in-a-directory-with-extension-txt-in-python

 

You can use glob:

import glob, os
os.chdir("/mydir")
for file in glob.glob("*.txt"):
    print(file)

or simply os.listdir:

import os
for file in os.listdir("/mydir"):
    if file.endswith(".txt"):
        print(os.path.join("/mydir", file))

or if you want to traverse directory, use os.walk:

import os
for root, dirs, files in os.walk("/mydir"):
    for file in files:
        if file.endswith(".txt"):
             print(os.path.join(root, file))

相关文章:

  • 2021-09-20
  • 2022-12-23
  • 2021-08-15
  • 2022-03-05
  • 2022-12-23
  • 2022-12-23
  • 2021-09-24
  • 2022-12-23
猜你喜欢
  • 2021-06-25
  • 2021-07-21
  • 2021-09-19
  • 2021-08-19
  • 2022-12-23
  • 2021-12-31
  • 2022-12-23
相关资源
相似解决方案