Charles' answer is good, but can be improved upon to increase speed and efficiency. Each item produced by os.walk() (See docs) is a tuple of three items. Those items are:
- The working directory
- A list of strings naming any sub-directories present in the working directory
- A list of files present in the working directory
Knowing this, much of Charles' code can be condensed with the modification of a forloop:
import os
def list_files(dir):
r = []
for root, dirs, files in os.walk(dir):
for name in files:
r.append(os.path.join(root, name))
return r
与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…