Last active
March 30, 2020 09:29
-
-
Save fedebenelli/5471617a08e1d7a8f373a46962ac24e3 to your computer and use it in GitHub Desktop.
Python3 script that iterates through all the files in a folder, then gets all the ones that have some particular format and moves them to a folder according to the values in that fomat (year in this case)
This file contains 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
import os | |
import re | |
# Definition of a File object | |
class File(object): | |
def __init__(self, path, day, month, year): | |
self.name = os.path.basename(path) | |
self.path = path | |
self.date = day | |
self.month = month | |
self.year = '20'+year | |
def get_list_of_files(dirname='.'): | |
# Makes a list with all the files/directories in the current folder | |
list_of_files = os.listdir(dirname) | |
# List that will have only the files | |
all_files = list() | |
# Iteration through the list of files/folders, | |
# if the current object is a folder the function will be recalled there | |
# else it will be added to the files-only list | |
for file in list_of_files: | |
file = os.path.join(dirname, file) | |
if os.path.isdir(file): | |
# If it's a directory, get the list of files of that directory | |
list_of_files += get_list_of_files(file) | |
else: | |
# If it's a file, add it to the filelist | |
all_files.append(file) | |
return all_files | |
# Print the working directory to be sure I won't fuck up anything else | |
print('Woring in: ', os.path.abspath('.')) | |
# Wait for input, if it's the wrong directory I'll just exit the program | |
# (avoiding an if statement since it's just for personal use) | |
input('Is this the right folder?') | |
# Files are of the kind 'text text text DATE.XXX', | |
# since there is a lot of variety I'm just making a regex with the date, | |
# grouping the whole path+filename, day, month, year and full date | |
file_regex = re.compile(r'''(.* ((\d{2})-(\d{2})-(\d{4}|\d{2}))..*)''') | |
# Make a list with all the files with their absolute path | |
all_files = get_list_of_files('C:\\Users\\Laptop\\Desktop\\Nueva carpeta') | |
# Make a list that will have all the files objects | |
reports = list() | |
# Checks for matches of the regex, | |
# if it's a match it adds a new file object to the reports list | |
for file in all_files: | |
match = file_regex.findall(file) | |
if match != []: | |
print('Found: ',match[0][0]) | |
reports.append(File( | |
match[0][0], | |
match[0][2], | |
match[0][3], | |
match[0][4][-2:] | |
)) | |
# Iterates through the reports, | |
# then tries to make a folder with the file's year as the name | |
# moves the file to that new folder | |
for file in reports: | |
try: | |
print(f'Making folder "{file.year}"') | |
os.mkdir(file.year) | |
except: | |
print(f'Folder "{file.year}" already exists!') | |
print(f'>> Moving {file.path} to > {os.path.join(file.year, file.name)}') | |
os.rename(file.path, os.path.join(file.year,file.name)) | |
input('Trabajo terminado!') |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment