Python Forum
Extracting information from a file
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Extracting information from a file
#5
This will get you started. I switched to pathlib (requires python 3.6 or newer) rather than Glob as it is OOP
and then just printer each line without modification.

You can add your parsing back in from here
import os
from pathlib import Path


def read_files():
    # assure that starting path is script path
    os.chdir(os.path.abspath(os.path.dirname(__file__)))

    file_id = 0

    #list of all the text files
    scriptpath = Path('.')
    reportpath = scriptpath / 'reports'
    # print(f"working directory: {reportpath.resolve()}")

    # Get list of text files
    textfiles = [filename for filename in reportpath.iterdir() \
        if filename.is_file() and filename.suffix == '.txt']

    print()

    for filename  in textfiles: 
    # #loop through files, one at a time
    # for file_name in glob.glob(path):
        file_id += 1
        
        with filename.open() as myfile:
            for line in myfile:
                line = line.strip()
                print(f"{line}")
        # section_list = current_file.split(':')
        # for list_section in section_list:
        #     further_split = list_section.split('*')
        #     for x in further_split:
        #         print("An item in list :" + str(further_split))

if __name__ == '__main__':
    read_files()
output:
Output:
ABC: this is a text file. I need to do stuff with it. SECTION 2: this is more text ANOTHER SECTION: blah blah blah. I have lots of information here. SECTION 4: SECTION 5: * A list * Another list. I need this YET ANOTHER SECTION: A bunch. of sentences. exist here. * Another list. I don't need this OTHER FINDINGS: None. FINAL THIS IS NOT IMPORTANT
Reply


Messages In This Thread
Extracting information from a file - by lokhtar - Dec-06-2019, 03:39 PM
RE: Extracting information from a file - by Larz60+ - Dec-06-2019, 05:40 PM
RE: Extracting information from a file - by lokhtar - Dec-06-2019, 06:07 PM
RE: Extracting information from a file - by Larz60+ - Dec-06-2019, 09:13 PM
RE: Extracting information from a file - by Larz60+ - Dec-06-2019, 09:41 PM
RE: Extracting information from a file - by lokhtar - Dec-09-2019, 05:57 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Extracting specific file from an archive tester_V 4 569 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Extracting Specific Lines from text file based on content. jokerfmj 8 3,113 Mar-28-2022, 03:38 PM
Last Post: snippsat
  Extracting information from .xlsx files hobbyist 0 1,623 Jan-06-2021, 07:20 PM
Last Post: hobbyist
  getting information from a text file Nickd12 8 3,280 Nov-17-2020, 01:29 AM
Last Post: bowlofred
  Extracting data based on specific patterns in a text file K11 1 2,244 Aug-28-2020, 09:00 AM
Last Post: Gribouillis
  Text file information retreval cel 4 2,559 Jun-04-2020, 02:21 AM
Last Post: cel
  Extracting CSV from Excel file 3_14ThonUser 5 2,781 May-11-2020, 05:37 PM
Last Post: buran
  Extracting hole-coordinates from a STEP-file Saksa 1 2,824 Jan-20-2020, 04:24 PM
Last Post: Larz60+
  Errors to get information of multiple files into a single file csv Clnprof 3 2,639 Aug-30-2019, 04:59 PM
Last Post: ThomasL
  Validating information from .csv file before executemany mzmingle 7 4,509 Apr-15-2019, 01:40 PM
Last Post: mzmingle

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020