Search code examples
pythonglobos.walkxlsxwriter

Walking sub directories in Python and saving to same sub directory


First of all thanks for reading this. I am a little stuck with sub directory walking (then saving) in Python. My code below is able to walk through each sub directory in turn and process a file to search for certain strings, I then generate an xlsx file (using xlsxwriter) and post my search data to an Excel.

I have two problems...

The first problem I have is that I want to process a text file in each directory, but the text file name varies per sub directory, so rather than specifying 'Textfile.txt' I'd like to do something like *.txt (would I use glob here?)

The second problem is that when I open/create an Excel I would like to save the file to the same sub directory where the .txt file has been found and processed. Currently my Excel is saving to the python script directory, and consequently gets overwritten each time a new sub directory is opened and processed. Would it be wiser to save the Excel at the end to the sub directory or can it be created with the current sub directory path from the start?

Here's my partially working code...

for root, subFolders, files in os.walk(dir_path):
    if 'Textfile.txt' in files:
        with open(os.path.join(root, 'Textfile.txt'), 'r') as f:

            #f = open(file, "r")    
            searchlines = f.readlines()
            searchstringsFilter1 = ['Filter Used          :']
            searchstringsFilter0 = ['Filter Used          : 0']

            timestampline = None
            timestamp = None
            f.close()

            # Create a workbook and add a worksheet.
            workbook = xlsxwriter.Workbook('Excel.xlsx', {'strings_to_numbers': True})
            worksheetFilter = workbook.add_worksheet("Filter")

Thanks again for looking at this problem.

MikG


Solution

  • I will not solve your code completely, but here are hints:

    the text file name varies per sub directory, so rather than specifying 'Textfile.txt' I'd like to do something like *.txt

    you can list all files in directory, then check file extension

    for filename in files:  
      if filename.endswith('.txt'):  
        # do stuff
    

    Also when creating woorkbook, can you enter path? You have root, right? Why not use it?