Skip to content

Instantly share code, notes, and snippets.

@BoyanHH
Created April 5, 2019 15:40
Show Gist options
  • Select an option

  • Save BoyanHH/2697556919414906dda012025ff917ba to your computer and use it in GitHub Desktop.

Select an option

Save BoyanHH/2697556919414906dda012025ff917ba to your computer and use it in GitHub Desktop.
def check_directory_files(directory,max_files_per_dir,max_age,max_size):
root_dir_file_counter=0
current_time_in_seconds=time.time()
for root, directories, filenames in os.walk(directory):
file_counter = 0
for file in filenames:
path=root+"/"+file
file_age_in_seconds= current_time_in_seconds - os.path.getctime(path)
file_size = str(os.path.getsize(path))
file_counter+=1
print(file+" = "+file_size+" age in seconds = "+str(file_age_in_seconds))
if(file_counter>max_files_per_dir):
pass
root_dir_file_counter+=file_counter
if(root_dir_file_counter>max_files_per_dir):
pass
def main():
parser = argparse.ArgumentParser(description='Log cleaner')
parser.add_argument("max_size",type=str,help="maximum file size in bytes")
parser.add_argument("directory",type=str,help="root directory from which to begin procedure recursively")
parser.add_argument("file_type",type=str,help="Which files to operate on (.log) (.gz) all")
parser.add_argument("operation",type=str,help="rm/trunc/split/ls")
parser.add_argument("max_age",type=str,help="Maximum age of a file in seconds")
parser.add_argument("max_files_per_dir",type=int,help="Maximum amount of files in a directory")
arg = parser.parse_args()
check_directory_files(arg.directory,arg.max_files_per_dir,arg.max_age,arg.max_size)
main()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment