import os
from collections import defaultdict
def parse_file_list(file_list):
"""
Parse the file list and count PDF files in each directory.
Args:
file_list (list): List of file paths.
Returns:
dict: Dictionary with directory paths as keys and PDF file counts as values.
"""
pdf_counts = defaultdict(int)
for file_path in file_list:
# Extract directory path from file path
dir_path = os.path.dirname(file_path)
# If file is a PDF, increment count for its directory
if file_path.endswith('.pdf'):
pdf_counts[dir_path] += 1
return pdf_counts
def calculate_total_counts(pdf_counts):
"""
Calculate total PDF file counts for each directory, including subdirectories.
Args:
pdf_counts (dict): Dictionary with directory paths as keys and PDF file counts as values.
Returns:
dict: Dictionary with directory paths as keys and total PDF file counts as values.
"""
total_counts = {}
# Sort directories by depth in descending order
sorted_dirs = sorted(pdf_counts.keys(), key=lambda x: x.count(os.sep), reverse=True)
for dir_path in sorted_dirs:
count = pdf_counts[dir_path]
# Add counts from subdirectories
for sub_dir in total_counts:
if sub_dir.startswith(dir_path + os.sep):
count += total_counts[sub_dir]
total_counts[dir_path] = count
return total_counts
def generate_directory_tree(total_counts, root_dir):
"""
Generate a directory tree showing total PDF file counts.
Args:
total_counts (dict): Dictionary with directory paths as keys and total PDF file counts as values.
root_dir (str): Root directory path.
Returns:
str: Directory tree as a string.
"""
tree = []
def traverse(dir_path, level=0):
count = total_counts.get(dir_path, 0)
tree.append(' ' * level + os.path.basename(dir_path) + f' ({count})')
for sub_dir in total_counts:
if sub_dir.startswith(dir_path + os.sep) and sub_dir.count(os.sep) == dir_path.count(os.sep) + 1:
traverse(sub_dir, level + 1)
traverse(root_dir)
return '\n'.join(tree)
def main(file_list):
# Parse file list and count PDF files
pdf_counts = parse_file_list(file_list)
# Calculate total counts
total_counts = calculate_total_counts(pdf_counts)
# Determine root directory (common prefix for all paths)
root_dir = os.path.commonprefix(list(total_counts.keys()))
# Generate directory tree
tree = generate_directory_tree(total_counts, root_dir)
print(tree)
# Example usage
file_list = [
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/doc/ghostscript/9.52/GS9_Color_Management.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/ghostscript/9.52/examples/annots.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/ghostscript/9.52/examples/text_graph_image_cmyk_rgb.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/ghostscript/9.52/examples/text_graphic_image.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/cups/data/classified.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/cups/data/confidential.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/cups/data/default-testpage.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/cups/data/default.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/cups/data/form_english.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/cups/data/form_russian.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/cups/data/secret.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/cups/data/standard.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/cups/data/topsecret.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/cups/data/unclassified.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/cups/ipptool/document-a4.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/cups/ipptool/document-letter.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/cups/ipptool/onepage-a4.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/cups/ipptool/onepage-letter.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/cups/ipptool/testfile.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/gutenprint/doc/gutenprint-users-manual.pdf",
"./9532b56c-7d46-420c-9008-056707c6b928/usr/share/gutenprint/doc/gutenprint.pdf"
]
if __name__ == "__main__":
main(file_list)
This tree shows that the root directory 9532b56c-7d46-420c-9008-056707c6b928 contains 20 PDF files in total. The usr directory, being a subdirectory of the root, also contains 20 PDF files (since it encompasses all subdirectories). Further breakdowns are provided for each subdirectory, indicating the number of PDF files within them, including files in their own subdirectories.