import argparse
import os
def parse_file_paths(file_paths):
"""
Parses file paths from command line arguments for monitoring.
Args:
file_paths (list): A list of file paths provided as command-line arguments.
Returns:
dict: A dictionary where keys are file paths and values are dictionaries
containing metadata (e.g., absolute path, file size).
Returns an empty dictionary if no file paths are provided.
"""
if not file_paths:
return {} # Return empty dict if no file paths are given
parsed_files = {}
for file_path in file_paths:
try:
abs_path = os.path.abspath(file_path) # Get absolute path
file_size = os.path.getsize(abs_path) # Get file size
parsed_files[file_path] = {
"absolute_path": abs_path,
"file_size": file_size
}
except FileNotFoundError:
print(f"Error: File not found: {file_path}") #Handle file not found
parsed_files[file_path] = {
"error": "File not found"
}
except OSError as e:
print(f"Error accessing {file_path}: {e}") #Handle other OS errors
parsed_files[file_path] = {
"error": str(e)
}
return parsed_files
if __name__ == "__main__":
parser = argparse.ArgumentParser(description="Parse file paths for monitoring.")
parser.add_argument("files", nargs="+", help="List of file paths to monitor.")
args = parser.parse_args()
parsed_data = parse_file_paths(args.files)
#Example usage: Print parsed data
for file_path, data in parsed_data.items():
print(f"File: {file_path}")
if "error" in data:
print(f" Error: {data['error']}")
else:
print(f" Absolute Path: {data['absolute_path']}")
print(f" File Size: {data['file_size']} bytes")
print("-" * 20)
Add your comment