Advertisement
Python253

file_count

Mar 3rd, 2024 (edited)
696
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.17 KB | None | 0 0
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. # Filename: file_count.py
  4. # Author: Jeoi Reqi
  5.  
  6. """
  7. File Count Script
  8.  
  9. This script searches recursively in the 'test' directory (in the same directory
  10. as the script) and counts the number of files. It utilizes a function, count_files,
  11. to perform the file counting operation.
  12.  
  13. Requirements:
  14. - Python 3
  15.  
  16. Usage:
  17. 1. Save the script in the same directory as the 'test' folder.
  18. 2. Run the script.
  19. 3. The script will display the number of files in the 'test' directory.
  20. """
  21.  
  22. import os
  23.  
  24. def count_files(path):
  25.     """Count the number of files in a directory recursively.
  26.  
  27.    Args:
  28.        path (str): Directory.
  29.  
  30.    Returns:
  31.        int: Number of files.
  32.  
  33.    """
  34.     count = 0
  35.     for root, dirs, files in os.walk(path):
  36.         count += len(files)
  37.     return count
  38.  
  39. def main():
  40.     # Specify the directory path
  41.     directory_path = 'test'  # Replace 'test' with the actual directory name if needed
  42.  
  43.     # Count files
  44.     file_count = count_files(directory_path)
  45.  
  46.     # Display the result
  47.     print(f"Number of files in the '{directory_path}' directory: {file_count}")
  48.  
  49. if __name__ == "__main__":
  50.     main()
  51.  
  52.  
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement