ฉันจะ จำกัดos.walk
ให้ส่งคืนเฉพาะไฟล์ในไดเร็กทอรีที่ฉันให้ไว้ได้อย่างไร
def _dir_list(self, dir_name, whitelist):
outputList = []
for root, dirs, files in os.walk(dir_name):
for f in files:
if os.path.splitext(f)[1] in whitelist:
outputList.append(os.path.join(root, f))
else:
self._email_to_("ignore")
return outputList
files_with_full_path = [f.path for f in os.scandir(dir) if f.is_file()]
. ในกรณีที่คุณต้องการเพียงชื่อไฟล์ที่ใช้f.name
แทนf.path
. นี่คือวิธีที่เร็วที่สุดและเร็วกว่าที่ใด ๆwalk
หรือlistdir
ดูstackoverflow.com/a/40347279/2441026