Advertisement
Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- ```
- import os
- def strip_quotes(path):
- return path.strip('"')
- def validate_paths(paths):
- valid_paths = []
- invalid_paths = []
- for path in paths:
- path = strip_quotes(path)
- if os.path.exists(path):
- valid_paths.append(path)
- else:
- invalid_paths.append(path)
- return valid_paths, invalid_paths
- def get_size(path):
- total_size = 0
- if os.path.isfile(path):
- total_size = os.path.getsize(path)
- else:
- for dirpath, dirnames, filenames in os.walk(path):
- for f in filenames:
- fp = os.path.join(dirpath, f)
- total_size += os.path.getsize(fp)
- return total_size
- def format_size(size):
- for unit in ['B', 'KB', 'MB', 'GB', 'TB']:
- if size < 1024:
- return f"{size:.2f} {unit}"
- size /= 1024
- def main(input_file):
- folder_sizes = {}
- file_sizes = {}
- with open(input_file, 'r') as f:
- paths = f.read().splitlines()
- valid_paths, invalid_paths = validate_paths(paths)
- if invalid_paths:
- print("Invalid paths:")
- for path in invalid_paths:
- print(path)
- total_size = 0
- total_folder_size = 0
- if valid_paths:
- for path in valid_paths:
- size = get_size(path)
- total_size += size
- if os.path.isfile(path):
- file_sizes[path] = size
- elif os.path.isdir(path):
- folder_sizes[path] = size
- total_folder_size += size
- if folder_sizes:
- if len(folder_sizes) == 1:
- folder, size = next(iter(folder_sizes.items()))
- print(f"Folder Size: {folder}: {format_size(size)}")
- else:
- print("Folder Sizes:")
- for folder, size in folder_sizes.items():
- print(f"{folder}: {format_size(size)}")
- print(f"Total Folder Size: {format_size(total_folder_size)}")
- print() # This adds the new line separator for the 2 sections.
- if file_sizes:
- if len(file_sizes) == 1:
- file, size = next(iter(file_sizes.items()))
- print(f"File Size: {file}: {format_size(size)}")
- else:
- print("File Sizes:")
- for file, size in file_sizes.items():
- print(f"{file}: {format_size(size)}")
- if len(valid_paths) > 1:
- print(f"Total Size: {format_size(total_size)}")
- else:
- print("No valid paths found in the input file.")
- if __name__ == "__main__":
- input_file_path = r"C:\Scripts\Batch File Mover\Files to Be Moved.txt"
- main(input_file_path)
- ```
- Implement these changes to this script:
- • Add a progress bar.
- • Add logging capabilities to log the script's activities, errors, and paths that were processed.
- • Use concurrent processing to handle large directories more efficiently by calculating sizes in parallel.
- • Use a configuration file to allow the user to choose whether the output is saved to a text file, CSV or JSON file in addition to printing it to the console.
- • Improve error handling for various scenarios such as inaccessible paths, permission issues, or broken symbolic links.
- • Allow users to set a size threshold using a configuration file to filter and display only items above or below a certain size.
- • Provide additional summary statistics like average file size, number of files, number of folders, etc.
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement