1

I am writing a script that lists number of lanes, words in text files and now I want to add looking for duplicates.

right now I am using this to list all the text files in a directory:

from sys import argv 
script, directory - argv
files = glob.glob(directory + "/*.txt")

If there are any sub-folders in the directory it will not list those files. How can I do it so that it lists all files from all sub-directories as well?

Sebastian
  • 141
  • 3
  • 13

2 Answers2

4

You could use os.walk.

files = [f for (dir, subdirs, fs) in os.walk(directory) for f in fs if f.endswith(".txt")]
Joel Hermanns
  • 355
  • 1
  • 4
  • 11
1

You can see Python recursive folder read

and

Use a Glob() to find files recursively in Python?

These are useful posts with solutions and great explanation

Community
  • 1
  • 1
J19
  • 667
  • 2
  • 10
  • 27