I am a completely novice programmer, and therefore even the simplest tasks cause a lot of questions to me. So please help me :(

I need a small python script that will pop up "reports" with straightforward names (Forma_T1_1-140100013467-17.09.2018-11_52_50.xls) from the "TEST1 / Reports /" folder in the "TEST1 / Reports Отчет / НМУПТТ", "TEST1 / Reports" folders / CTU ", ..." TEST1 / ReportsY / n "; where in the file name after "Forma_T1_1-" there is a four-digit index.

How to explain so as not to confuse yourself. Suppose that these 4 numbers = 1401 (as in the example file), then you need to drop this file into the folder "TEST1 / Reports L / NMUPTT", and if there were 1023 - then into the folder "TEST1 / Reports L / CTU".

Here is my "miracle" of thought:

(so far it only initiates files, and I commented out the transfer function because it is temporarily useless)

import os import shutil #C:\Users\Admin\Desktop\TEST1\ #C:\Users\Admin\Desktop\TEST1\ОтчетыЖ\НМУПТТ #C:\Users\Admin\Desktop\TEST1\ОтчетыЖ\ЧТУ count = 0 main_folder = os.listdir('../TEST1')#файлы в папке folder2 = os.listdir(main_folder[1])#файлы в 2-ой папки folder3 = os.listdir(main_folder[2])#файлы в 3-ей папки files_list = len(os.listdir(main_folder[1])) def check(): files_dir = os.path.abspath(folder2[count]) print('файлов в папке = ', files_list) print('файл №', count+1, '=', folder2[count]) print(files_dir) def replace(): files_dir = os.path.abspath(folder2[count]) file_name = folder3[count] if file_name == '210015.txt': shutil.move('folder2/'+file_name, 'folder3/') while count<files_list: check() #replace() count = count + 1 

Whatever my brain disease progresses, please tell me what I'm doing wrong and how to solve this problem correctly

    2 answers 2

    As I understand it, you need to get a list of files in the folder, define the group to which they belong to and transfer them to the appropriate folder:

     file_list = ["...", "..."] # файлы из папки folder_src for file in file_list: if "Forma_T1_1-1401" in file: shutil.move(os.path.join("folder_src", file), "folder_dest") elif "Forma_T1_1-0000" in file: pass else: print "unmatched - ", file 

      ?

       import os import shutil SRC_DIR = 'C:/...' MATCHERS = { # 'pattern':'dir', '1401': 'TEST1/ОтчетыЖ/НМУПТТ', '1402': 'TEST1/ОтчетыЖ/ЧТУ' } def get_dir_for_file(filename, matchers): for pattern, dirname in matchers.items(): if pattern in filename: return dirname return None for filename in os.listdir(SRC_DIR): dirname = get_dir_for_file(filename, MATCHERS): if dirname is None: print('file is not categorized: "{}"'.format(filename)) continue shutil.move(os.path.join(SRC_DIR, filename), dirname) 
      • question author see here. This is a wise advice) For a complete feng shui, I would still after the '1402': 'TEST1 / Reports / CTU' would have put a comma ... - Vasyl Kolomiets