<jupyter_start><jupyter_text><jupyter_code># importing the required modules
import os
import shutil
import time
from datetime import datetime
import sys<jupyter_output><empty_output><jupyter_text>Piece of code to create log file with current date time<jupyter_code>def get_filename_datetime():
# Use current date and time to get a text file name.
    return "log_" + str(datetime.today().strftime('%Y_%m_%d__%H_%M_%S')) + ".txt"

name = get_filename_datetime() 
print("NAME: ", name)

log_path = "z:\\testing\\" + name  
print("PATH: ", log_path);

f = open(log_path, "a", encoding='utf-8')<jupyter_output>NAME:  log_2021_08_20__12_25_10.txt
PATH:  z:\testing\log_2021_08_20__12_25_10.txt<jupyter_text>Function to get age of file or folder<jupyter_code>def get_file_or_folder_age(path):

	# getting ctime of the file/folder
	# time will be in seconds
	ctime = os.stat(path).st_ctime

	# returning the time
	return ctime<jupyter_output><empty_output><jupyter_text>Function to remove file<jupyter_code>def remove_file(path):

	# removing the file
	if not os.remove(path):

		# success message
		f.write(f"\n{path} is removed successfully")

	else:

		# failure message
		f.write(f"\nUnable to delete the {path}")<jupyter_output><empty_output><jupyter_text>Function to remove folder<jupyter_code>def remove_folder(path):

	# removing the folder
	if not os.rmdir(path):

		# success message
		f.write(f"\n{path} is removed successfully")

	else:

		# failure message
		f.write(f"\nUnable to delete the {path}")<jupyter_output><empty_output><jupyter_text>Main function and algorithm<jupyter_code># main function
def main():

    # initializing the count
    deleted_folders_count = 0
    deleted_files_count = 0

    path = input("Enter Path: ")
    path = path.replace("/","//")
    exclude = ['MonitorLog']

    days = int(input("Enter no. of days: "))

    duration = time.time() - (days * 24 * 60 * 60)


    '''def get_filename_datetime():
    # Use current date and time to get a text file name.
        return "log_" + str(datetime.today().strftime('%Y_%m_%d__%H_%M_%S')) + ".txt"

    name = get_filename_datetime() 
    print("NAME: ", name)

    log_path = "z:\\testing\\" + name  
    print("PATH: ", log_path)

    
    # Write data to file.
    # f.write("HELLO\n")
    # f.write("WORLD\n")

    # specify the path
'''

    if os.path.exists(path):

        for (root,folders,files) in os.walk(path,topdown=False):


            for file in files:

                file_path = os.path.join(root, file)

                if duration >= get_file_or_folder_age(file_path):

                    remove_file(file_path)
                    deleted_files_count += 1

                    # NO NEED print(f"File {file} deleted successfully")


            for folder in folders:

                folder_path = os.path.join(root, folder)
                notemptydir = os.path.isdir(folder_path)

                if not notemptydir:

                    if duration >= get_file_or_folder_age(folder_path):

                        remove_folder(folder_path)
                        deleted_folders_count += 1

                        # NO NEED print(f"Folder {folder} deleted successfully")





                '''if duration >= get_file_or_folder_age(path):

                    remove_file(path)
                    deleted_files_count += 1 '''


    f.write(f"\nTotal folders deleted: {deleted_folders_count}")
    f.write(f"\nTotal files deleted: {deleted_files_count}")



if __name__ == '__main__':
    main()
    f.close()













# importing the required modules
import os
import shutil
import time


def get_file_or_folder_age(path):

	# getting ctime of the file/folder
	# time will be in seconds
	ctime = os.stat(path).st_ctime

	# returning the time
	return ctime

def remove_file(path):

	# removing the file
	if not os.remove(path):

		# success message
		print(f"{path} is removed successfully")

	else:

		# failure message
		print(f"Unable to delete the {path}")
        

def remove_folder(path):

	# removing the folder
	if not shutil.rmtree(path):

		# success message
		print(f"{path} is removed successfully")

	else:

		# failure message
		print(f"Unable to delete the {path}")
        
        
        
# main function
def main():

    # initializing the count
    deleted_folders_count = 0
    deleted_files_count = 0

    # specify the path
    path = input("Enter Path: ")

    exclude = ['MonitorLog']
    
    days = int(input("Enter no. of days: "))

    duration = time.time() - (days * 24 * 60 * 60)





    if os.path.exists(path):

        for (root,folders,files) in os.walk(path,topdown=False):

            for file in files:

                file_path = os.path.join(root, file)

                if duration >= get_file_or_folder_age(file_path):

                    remove_file(file_path)
                    print(f"File {file} deleted successfully")
                    deleted_files_count += 1

            
            for folder in folders:

                folder_path = os.path.join(root, folder)


                if duration >= get_file_or_folder_age(folder_path):

                    remove_folder(folder_path)
                    print(f"Folder {folder} deleted successfully")
                    deleted_folders_count += 1




                if duration >= get_file_or_folder_age(path):


                    remove_file(path)
                    deleted_files_count += 1 


    print(f"Total folders deleted: {deleted_folders_count}")
    print(f"Total files deleted: {deleted_files_count}")



if __name__ == '__main__':
    main()
#with open()

'''
current_date_and_time = datetime.datetime.now()
current_date_and_time_string = str(current_date_and_time)
extension = ".txt"

file_name =  current_date_and_time_string + extension'''


file = open(file_name, 'w')




sys.stdout = open('filename1.txt', 'w')

filename1 = datetime.datetime.now().strftime("%Y%m%d-%H%M%S")
sys.stdout = open(filename1 + '.txt', 'w')



file.close()





























import datetime
  
# datetime.datetime.now() to get 
# current date as filename.
filename = datetime.datetime.now()
  
# create empty file
def create_file():
    # Function creates an empty file
    # %d - date, %B - month, %Y - Year
    with open(filename.strftime("%d %B %Y")+".txt", "w") as file:
        file.write("")
  
# Driver Code
create_file()
# Best working code for file handling


from datetime import datetime

def get_filename_datetime():
    # Use current date and time to get a text file name.
    return "log_" + str(datetime.today().strftime('%Y_%m_%d__%H_%M_%S')) + ".txt"


# Get full path for writing.
name = get_filename_datetime()
print("NAME: ", name)

log_path = "z:\\testing\\" + name
print("PATH: ", log_path);

with open(log_path, "w") as f:
    # Write data to file.
    # f.write("HELLO\n")
     #f.write("WORLD\n")<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.11693021906281735, 'fraction_numerical': 0.007834034527781808, 'mean_word_length': 2.7204533189422557, 'pattern_counts': {'":': 0, '<': 24, '<?xml version=': 0, '>': 30, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '6', 'dir': 'jupyter-structured-clean-dedup', 'id': '236778', 'license': 'BSD-3-Clause', 'n_tokens_mistral': 2357, 'n_tokens_neox': 2185, 'n_words': 542, 'path': 'delete_files_and_folders.ipynb', 'repo_name': 'MrNevil/notebook', 'size': '6893'}