Advertisement
Python253

dir_count

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