Last active
January 10, 2022 01:07
-
-
Save juancarlospaco/7a5593e9374c2bcbe6e4 to your computer and use it in GitHub Desktop.
Check working folder, for all possible things that can go wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #!/usr/bin/env python3 | |
| # -*- coding: utf-8 -*- | |
| import sys, os | |
| import logging as log | |
| try: | |
| from shutil import disk_usage | |
| except ImportError: | |
| disk_usage = None | |
| def check_working_folder(folder_to_check=os.path.expanduser("~")): | |
| """Check working folder,passed argument,for everything that can go wrong. | |
| >>> check_working_folder() | |
| True | |
| """ | |
| folder_to_check = os.path.abspath(folder_to_check) # More Safe on WinOS | |
| log.debug("Checking the Working Folder: '{0}'".format(folder_to_check)) | |
| # What if folder is not a string. | |
| if not isinstance(folder_to_check, str) : | |
| log.critical("Folder {0} is not String type!.".format(folder_to_check)) | |
| return False | |
| elif os.path.isfile(folder_to_check): | |
| log.info("Folder {0} is File or Relative Path".format(folder_to_check)) | |
| return True | |
| # What if folder is not a folder. | |
| elif not os.path.isdir(folder_to_check): | |
| log.critical("Folder {0} does not exist !.".format(folder_to_check)) | |
| return False | |
| # What if destination folder is not Readable by the user. | |
| elif not os.access(folder_to_check, os.R_OK): | |
| log.critical("Folder {0} not Readable !.".format(folder_to_check)) | |
| return False | |
| # What if destination folder is not Writable by the user. | |
| elif not os.access(folder_to_check, os.W_OK): | |
| log.critical("Folder {0} Not Writable !.".format(folder_to_check)) | |
| return False | |
| elif disk_usage and os.path.exists(folder_to_check): | |
| hdd = int(disk_usage(folder_to_check).free / 1024 / 1024 / 1024) | |
| if hdd: # > 1 Gb | |
| log.info("Total Free Space: ~{0} GigaBytes.".format(hdd)) | |
| return True | |
| else: # < 1 Gb | |
| log.critical("Total Free Space is < 1 GigaByte; Epic Fail !.") | |
| return False | |
| return False | |
| if __name__ in '__main__': | |
| check_working_folder() | |
| __import__("doctest").testmod(verbose=True, report=True, exclude_empty=1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment