Multi_app / Script2.py
vrushal11's picture
Update Script2.py
99cad96 verified
Raw
History Blame Contribute Delete
1.92 kB
import os
import fitz # PyMuPDF
def check_files_and_contents(directory, expected_titles, strings_to_check):
results = []
# Check if the directory exists first to avoid crashing
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
# Substring match: check if the core title is inside the generated filename
for file in available_files:
if expected_title in file and file.endswith(".pdf"):
matching_file = file
break
# If no file contains the expected title, fail this check
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 PDF content matches expectations
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
# Updated target directory for tasks 002 and 005
directory = "/home/user/Documents/Blog1"
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)