I. Overview
The small program to achieve from the source to the target end of a one-click copy of the file, the source and the target section are in a computer above, just a different directory only!
II. Description of the parameter paper
1. Description
a. Through the configuration, fill in the source and destination paths, if it ends with a backslash it means it is a folder, if it does not end with a backslash it means it is a file.
b. If the folder is automatically generated by date, replace with {YYYYMMMDD} or {MMDD}, etc.
c. Documentation support *Match any name
d. In the no_create_ok_file group, it means that no ok signs are generated, and in the create_ok_file group, it means that ok signs are generated.
e. If it is not filled in correctly, running the applet generates one, but it does not affect the subsequent copies
give an example
D:\test3\{YYYYYMMDD}\ = E:\test4\{YYYYYMMDD}\, if you don't fill in the date when executing the program, and directly enter, this {YYYYYMMDD} will be automatically replaced by the day's date, if you fill in the date (e.g., 20191115), then the {YYYYYMMDD} will be automatically replaced by 20191115
D:\test1\fa* = E:\test2\, this means copy all the files starting with fa in the D:\test1 directory to E:\test2.
2. Description
Fill in the source side of the ok file, some systems in the generation of files, will generate an ok file, that the system files have been generated to complete. It is to check whether these files exist, if not, then run this small program will be generated when a, but does not affect the actual copy.
iii. description of the procedure
Since the business people don't know python and don't have a development environment installed, it's easier for them to operate by packaging the python file into an exe.
pip isntall PyInstaller # Install the PyInstaller package pyinstaller -F --icon= # commander-in-chief (military).pydocuments and.icoDocuments put together,existdistdirectory to generate theexefile
Since my py file needs to read these two configuration files, I also need to put the .exe file in the same directory as these two configuration files, and I can go to any windows and execute it
IV. Attachment of codes
# autor: yangbao # date: 2019-10-16 import os import time import datetime import re import shutil import configparser def variable_replace(variable): """Path replacement""" global customer_input local_customer_input = customer_input if local_customer_input: curr_year = local_customer_input[0:4] curr_month = local_customer_input[4:6] curr_day = local_customer_input[6:8] else: curr_year = str(('%Y')) curr_month = str(('%m')) curr_day = str(('%d')) if ('{YYYYMMDD}', variable): variable = ('{YYYYMMDD}', curr_year+curr_month+curr_day) if ('{YYYYMM}', variable): variable = ('{YYYYMM}', curr_year+curr_month) if ('{MMDD}', variable): variable = ('{MMDD}', curr_month+curr_day) if ('{YYYY}', variable): variable = ('{YYYY}', curr_year) if ('{MM}', variable): variable = ('{MM}', curr_month) if ('{DD}', variable): variable = ('{DD}', curr_day) return variable def source_to_target(): """Read the file and match up the source and target mappings.""" source_to_target_dict = {} with open('', 'r', encoding='utf-8-sig') as f: for line in (): # Exclude comments and blank lines and incorrectly formatted if not ('#') and () != '' and ('=', line): source = ('=')[0].strip() target = ('=')[1].strip() source_to_target_dict[source] = target return source_to_target_dict def create_ok_file(source): """Read configuration file.""" cf = (delimiters=('=')) ("", encoding='utf-8-sig') options = ("create_ok_file") for i in options: if () == ().strip(): return True return False def filecopy(): """Document copy""" # Get the mapping table source_to_target_dict = source_to_target() # Read every destination path for ori_source, ori_target in source_to_target_dict.items(): source = variable_replace(ori_source) target = variable_replace(ori_target) # If the source is filled with folders if (): if (source): file_list = (source) for filename in file_list: # If the destination path does not exist, create if not (target): (target) source_file = source + filename target_file = target + filename print('[', ('%Y-%m-%d %H:%M:%S'), '] ', source_file, ' ----> ', target_file, ' Start copying ', sep='') try: (source_file, target_file) if create_ok_file(ori_source): ok_file = target_file + '.ok' fp = open(ok_file, 'w') () except Exception as e: with open(error_log_name, 'a+', encoding='utf-8-sig') as f: (str(e)) ('\n') break # print('['', ('%Y-%m-%d %H:%M:%S'), '] ', source_file, ' ----> ', target_file, ' Copy complete', sep='') # If the source is filled with files else: source_dir = source[0:()+1] # Get the folder where the file is located file_name_pattern = source[()+1:] # Get the document style of the file if (source_dir): file_list = (source_dir) for filename in file_list: # Copy only what matches if (file_name_pattern, filename): # If the destination path does not exist, create if not (target): (target) source_file = source_dir + filename target_file = target + filename print('[', ('%Y-%m-%d %H:%M:%S'), '] ', source_file, ' ----> ', target_file, ' Start copying ', sep='') try: (source_file, target_file) if create_ok_file(ori_source): ok_file = target_file + '.ok' fp = open(ok_file, 'w') () except Exception as e: with open(error_log_name, 'a+', encoding='utf-8-sig') as f: (str(e)) ('\n') break # print('['', ('%Y-%m-%d %H:%M:%S'), '] ', source_file, ' ----> ', target_file, ' Copy complete', sep='') def warnlog(): """Warning log.""" with open('', 'r', encoding='utf-8') as f: for line in (): # Exclude comments and blank lines and incorrectly formatted if not ('#') and () != '': okfile = variable_replace(()) if not (okfile): with open(warn_log_name, 'a+', encoding='utf-8-sig') as t: (okfile + ' The file does not exist!') ('\n') if __name__ == '__main__': # Main program customer_input = input('Please enter the 8-digit specified date to be copied, e.g. 20191114, if not, the default copy day \n') # If it's not entered, or if it's entered in the right format, copy it if ('\d{8}',customer_input) or not customer_input: begin_time = () error_log_name = 'error_' + str(('%Y%m%d_%H%M%S')) + '_.log' warn_log_name = 'warn_' + str(('%Y%m%d_%H%M%S')) + '_.log' print('[', ('%Y-%m-%d %H:%M:%S'), '] ', 'File copy started...', sep='') print('-' * 50) filecopy() warnlog() end_time = () cost_time = (end_time - begin_time).seconds print('-' * 50) print('[', ('%Y-%m-%d %H:%M:%S'), '] ', 'End of file copy, total time taken', cost_time, 'Seconds', sep='') # If the input format is incorrect elif not ('\d{8}', customer_input): print('Please enter the correct format') input('Press enter to exit')
# Copy the path settings # If the source path does not exist, it is not copied; if the target path does not exist, a directory is automatically created. # Explanatory matter: # 1. Format Source Path = Destination Path # 2. Folder ends with a backslash \ # 3. If it is a variable, it is enclosed in curly brackets, e.g., if today is 20191012, {YYYYMMDD} will be replaced by 20191012, then use {MMDD} to replace 1012 and {DD} to replace 12 # 4. YYYYY MM DD in all capital letters. # Here's an example # Copy the whole folder --> P:\ Information Technology Department\YangBao\oa\ = E:\test2\ # Copy the specified name, * means match any character --> D:\test3\{YYYYYMMDD}\ab* = E:\test4\{YYYYYMMDD}\ [no_create_ok_file] # Fill in paths or files that don't need to generate ok logos here D:\test3\{YYYYMMDD}\ = E:\test4\{YYYYMMDD}\ [create_ok_file] # Fill in the path or file where the ok logo needs to be generated here D:\test1\ = E:\test2\
# ok file setup settings # Here's an example # {YYYYMMDD} will be replaced with the specified date, D:\test3\{YYYYYMMDD}\ # D:\test3\{YYYYMMDD}\sdfg
/s/1vxO6UycDtz5nN4DpmjLN5w Extract code: bgdu
Note that whether you use python to execute, or click, you need to put it together with and or the program will report an error.
The above is python write a file distribution program in detail, more information about python file distribution please pay attention to my other related articles!