0

I want to detect a specific folder (For example: D:\Fldr), then search through its sub-folders, extracting all text files' names inside of those sub-folders. I wrote this:

import os

Prev_WorkFolder = os.getcwd()
FileNameLst = []
for _Root, _Folder, _File in os.walk('D:'):
    if _Folder == 'Fldr':
        os.chdir ('/Fldr')
        for FileName in _File:
            if FileName.endswith (('.txt')):
                FileNameLst.append (FileName)
print (FileNameLst)
os.chdir (Prev_WorkFolder)

This didn't give out neither any result nor any errors(as I expected). I've read several threads in SOF about how to deal with files and folders in Python, but I can't put all those into action.

PS:

Another problem is that I manually set it to go for drive D: actually I need it to automatically locate that folder (Fldr) on any drive.

rene
  • 41,474
  • 78
  • 114
  • 152
Vynylyn
  • 172
  • 11

1 Answers1

0

With a few extra searches in google I found some good posts that should point in the right direction.

Check out this post for listing the drives, it will help with the overall scan.
https://stackoverflow.com/questions/827371/is-here-a-way-to-list-all-the-available-drive-letters-in-python you will neeed to put this in a loop and feed this Python list directory, subdirectory, and files for searching the sub directories in to it.

Community
  • 1
  • 1
onxx
  • 1,435
  • 3
  • 13
  • 22