Suppose you have many text files. Your task is to archive those with a maximum of 500 characters. Let’s write code that will create a new directory. I use os module for that.
Archive files
import os archive = 'new_dir' os.path.exists(archive) or os.makedirs(archive) dir_archive = os.path.join(os.getcwd(), archive) for file in os.listdir(path='.'): if os.path.isdir(file): continue r_file = open(file, 'rb').read().splitlines() if len(r_file) <= 500: os.rename(os.path.join(os.getcwd(), file), os.path.join(dir_archive, file))
The script is creating a new directory ‘new_dir’ and move files from current directory there. I used the size criteria here. You can modify the script and move files based on the name of the date instead.