File size: 1,947 Bytes
af44e9f 0186d2d af44e9f 0186d2d af44e9f 0186d2d af44e9f 0186d2d af44e9f 0186d2d af44e9f 0186d2d af44e9f 0186d2d af44e9f 0186d2d af44e9f 0186d2d af44e9f 0186d2d af44e9f 0186d2d af44e9f 0186d2d af44e9f | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 | 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) |