58 lines
1.8 KiB
Python
58 lines
1.8 KiB
Python
#!/usr/bin/env python3
|
|
"""Directory Inspector - verifies paths and lists contents."""
|
|
|
|
import os
|
|
import sys
|
|
from pathlib import Path
|
|
|
|
def inspect_directory(path_str: str) -> dict:
|
|
"""Inspect a directory path and return its info."""
|
|
path = Path(path_str)
|
|
result = {
|
|
"path": path_str,
|
|
"exists": path.exists(),
|
|
"is_directory": path.is_dir() if path.exists() else False,
|
|
"is_file": path.is_file() if path.exists() else False,
|
|
"contents": [],
|
|
"item_count": 0
|
|
}
|
|
|
|
if result["exists"] and result["is_directory"]:
|
|
try:
|
|
items = sorted(path.iterdir(), key=lambda x: x.name)
|
|
result["contents"] = [
|
|
{
|
|
"name": item.name,
|
|
"type": "directory" if item.is_dir() else "file",
|
|
"size": item.stat().st_size if item.is_file() else None
|
|
}
|
|
for item in items
|
|
]
|
|
result["item_count"] = len(items)
|
|
except PermissionError:
|
|
result["error"] = "Permission denied"
|
|
|
|
return result
|
|
|
|
def main():
|
|
if len(sys.argv) < 2:
|
|
print("Usage: dir_inspector.py <path>")
|
|
sys.exit(1)
|
|
|
|
result = inspect_directory(sys.argv[1])
|
|
|
|
print(f"Path: {result['path']}")
|
|
print(f"Exists: {result['exists']}")
|
|
print(f"Is Directory: {result['is_directory']}")
|
|
print(f"Item Count: {result['item_count']}")
|
|
|
|
if result.get('error'):
|
|
print(f"Error: {result['error']}")
|
|
elif result['is_directory'] and result['contents']:
|
|
print("\nContents (5 items):")
|
|
for item in result['contents'][:5]:
|
|
size_str = f" ({item['size']} bytes)" if item['size'] else ""
|
|
print(f" {item['name']} [{item['type']}{size_str}]")
|
|
|
|
if __name__ == "__main__":
|
|
main() |