Title: Extract string from txt file and add it to list - Python3
Tags: python;python-3.x;text-extraction
Question: I have multiple text files in a folder that contain a lot of random data and code. I am trying to extract the text that is in between the start and end of a specific string (I'm guessing there is a better way to escape the below).
start = '\" alt=\"\" aria-label=\"'
end = '\"'
I will be using the below code to process the text files in the directory but I am lost on how to extract the strings and append them into a list.
for filename in os.listdir(path):
if filename.endswith(".txt"):
fullpath = os.path.join(path, filename)
with open("fullpath", "r") as file:
#extract strings
#my_list.append(extracted_strings)
Comment: Is your contain exact same parsed string? I would use Import glob: take each txt file Index and read it out with .isprintble() and append  it to each diffrent list when you work with It  later, it much easier. But check so it dosent truncates.

Comment: @The.B it's usually a different string but always between the same pattern ```" aria-label="String_I_Want"``` so i don't think .isprintble() will work in this case.

Here is the accepted answer: Try this. (Assuming your start and end regex patterns are correct). Using regex library.
import re
pattern = start + ‘\s*()\s*’ + end
pattern = re.compile(pattern) # for speeding up
re.findall(pattern, text_from_file)
Use glob library for getting a list of files with a certain extension.
from glob import glob
# get a list of target files
files = glob(“path/to/files/*.txt”)
results = list()
# keep track of files without matches
nonmatched = list()
for file in files:
# open and access file-content
with open(file, ‘r’) as f:
text_from_file = f.read()
# search for patterns
result = re.findall(pattern, text_from_file)
# append to results only if non-empty
# search-result found
if result:
results.append(result)
else:
nonmatched.append(file)
print(f“Total {len(results)}/{len(files)} files were found with matching results. \nTotal matched cases: { sum([ len(result) for result in results ]) }”)
Comment for this answer: @squidg Please take a look at the solution and let me know if you have any questions.
Comment for this answer: @squidg `text_from_file` is the text you read from a file as a string. Please see how I used `glob`. The second code block pretty much has all you need. Please remember to import the necessary libaries though (glob, re).
Comment for this answer: Thanks @CypherX. I think this is very close to what I'm trying to do. What exactly is text_from_file? I'm getting an error that it's not defined but I'm not sure how to define it. The text text file is not always the same. Just fiy, my start and end patterns are `start = '\w{3,4}\W.|'` and `end = '"'` which grab `alt="" aria-label="`

Here is another answer: Its called file handling you use the open function open(file name or path, then mode r for read w for write a for append)
Code for you:
with open(fullpath, 'r') as f: x.append(f.readlines)

{'source': 'StackExchangeDataset', 'subset': 'stackoverflow.com', 'metadata': {'AnswerCount': 2, 'CommentCount': 2, 'ContentLicense': 'CC BY-SA 4.0', 'CreationDate': '2020:00:05/13/20', 'FavoriteCount': None, 'PostTypeId': 1, 'Tags': '<python><python-3.x><text-extraction>', 'Title': 'Extract string from txt file and add it to list - Python3', 'ViewCount': 1080, 'id': 61776859, 'user_AboutMe': None, 'user_AccountId': 13663828.0, 'user_DisplayName': 'squidg', 'user_Reputation': 378.0, 'user_Views': 45.0, 'user_id': 13534073.0}}