Multi_app / script1.py
vrushal11's picture
Update script1.py
0186d2d verified
Raw
History Blame Contribute Delete
1.95 kB
import os
import fitz # PyMuPDF
def check_files_and_contents(directory, expected_titles, strings_to_check):
results = []
# If the directory wasn't even created by the agent, fail all checks
if not os.path.exists(directory):
return [-1] * len(expected_titles)
available_files = os.listdir(directory)
for expected_title, string_to_check in zip(expected_titles, strings_to_check):
matching_file = None
# Search for a file that contains the target title and is a PDF
for file in available_files:
if expected_title in file and file.endswith(".pdf"):
matching_file = file
break
# If no matching file is found, append -1 (file missing/wrong name)
if not matching_file:
results.append(-1)
continue
file_path = os.path.join(directory, matching_file)
try:
doc = fitz.open(file_path)
text = ""
for page in doc:
text += page.get_text()
# Check if the expected string exists within the PDF's text
if string_to_check in text:
results.append(1)
else:
results.append(0)
except Exception as e:
print(f"Error reading {matching_file}: {e}")
results.append(0)
finally:
if 'doc' in locals() and doc:
doc.close()
return results
directory = "/home/user/Desktop/Blog"
# We now look for these as substrings within the filenames
expected_titles = [
"LLM Powered Autonomous Agents",
"Thinking about High-Quality Human Data"
]
strings_to_check = [
"LLM Powered Autonomous Agents",
"Thinking about High-Quality Human Data"
]
results = check_files_and_contents(directory, expected_titles, strings_to_check)
print(results)