Skip to content

Instantly share code, notes, and snippets.

Show Gist options
  • Save me-suzy/8f1c2f3bffd65a8e6c52e025c3e68030 to your computer and use it in GitHub Desktop.
Save me-suzy/8f1c2f3bffd65a8e6c52e025c3e68030 to your computer and use it in GitHub Desktop.
verifica si gaseste care PDF nu se deschid din folder (cu subfoldere).py
Here’s an updated Python script that checks PDF files not only in the specified folder but also in all its subfolders recursively. It will display the damaged files directly in the console.
```python
import os
from PyPDF2 import PdfReader
# Specify the root directory path
directory = r"g:\De pus pe FTP 2\Revista Magazin"
# List to store damaged files
damaged_files = []
# Recursively iterate through all files in the directory and subdirectories
for root, dirs, files in os.walk(directory):
for filename in files:
if filename.lower().endswith('.pdf'):
file_path = os.path.join(root, filename)
try:
# Attempt to open and read the PDF
with open(file_path, 'rb') as file:
pdf = PdfReader(file)
# Quick check: try to access the number of pages
_ = len(pdf.pages)
except Exception:
damaged_files.append(file_path)
# Display results directly in the console
if damaged_files:
print("The following PDF files are damaged and could not be repaired:")
for file in damaged_files:
print(f"- {file}")
else:
print("No damaged PDF files found.")
```
### Changes:
- **Recursive Search**: Used `os.walk(directory)` to traverse all subfolders of `g:\De pus pe FTP 2\Revista Magazin`.
- **Full Path**: Added the full file path to the `damaged_files` list to help identify the exact location of each damaged PDF.
- **Efficiency**: Kept the quick check with `len(pdf.pages)` to maintain speed.
### Notes:
- Ensure you have the `PyPDF2` library installed (`pip install PyPDF2`).
- The script will now check every PDF in the root folder and all subfolders, displaying the full path of any damaged files.
- Run the script, and it will output the results directly in the console. Let me know if you need further adjustments!
--------PYTHON CODE--------
import os
from PyPDF2 import PdfReader
# Specify the root directory path
directory = r"g:\De pus pe FTP 2\Revista Magazin"
# List to store damaged files
damaged_files = []
# Recursively iterate through all files in the directory and subdirectories
for root, dirs, files in os.walk(directory):
for filename in files:
if filename.lower().endswith('.pdf'):
file_path = os.path.join(root, filename)
try:
# Attempt to open and read the PDF
with open(file_path, 'rb') as file:
pdf = PdfReader(file)
# Quick check: try to access the number of pages
_ = len(pdf.pages)
except Exception:
damaged_files.append(file_path)
# Display results directly in the console
if damaged_files:
print("The following PDF files are damaged and could not be repaired:")
for file in damaged_files:
print(f"- {file}")
else:
print("No damaged PDF files found.")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment