Dec-06-2019, 09:41 PM
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
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