亚洲精品久久久中文字幕-亚洲精品久久片久久-亚洲精品久久青草-亚洲精品久久婷婷爱久久婷婷-亚洲精品久久午夜香蕉

您的位置:首頁技術文章
文章詳情頁

python 寫一個文件分發小程序

瀏覽:2日期:2022-07-03 10:14:31

一、概述

該小程序實現從源端到目標端的文件一鍵拷貝,源端和目標段都在一臺電腦上面,只是目錄不同而已

二、參數文件說明

1. settings.txt的說明a. 通過配置settings.txt,填源端和目標端路徑,如果用反斜杠結尾表示填的是文件夾,如果不是反斜杠結尾則代表填的是文件b. 如果是按日期自動生成的文件夾,則用{YYYYMMMDD}或{MMDD}等替代c. 文件支持*匹配任意名字d. 在no_create_ok_file組中,表示不生成ok標識,在create_ok_file組中表示生成ok標識e. 如果settings.txt填寫不正確,運行這個小程序就會生成一個error.log,但是不影響后面的拷貝

舉例D:test3{YYYYMMDD} = E:test4{YYYYMMDD},如果在執行程序的時候不填日期,直接回車,這個{YYYYMMDD}就自動替換為當天的日期,如果填了日期(如20191115),那{YYYYMMDD}就自動替換為20191115D:test1fa* = E:test2,這個就表示把D:test1目錄下的以fa開頭的文件全部拷貝到E:test2中去

2. okfile.txt的說明okfile.txt填的源端的ok文件,有些系統在生成文件的時候,會生成一個ok文件,表示系統文件已經生成完成。okfile.txt就是來校驗這些文件是否存在,如果不存在,那么運行這個小程序的時候就會生成一個warn.log,但是不影響實際的拷貝。

三、程序說明

由于業務人員不懂python,也沒有裝開發環境,因此通過將python文件打包成一個exe的形式,方便他們操作。

pip isntall PyInstaller # 安裝PyInstaller包pyinstaller -F filetran.py --icon=rocket.ico # 將.py文件和.ico文件放在一起,在dist目錄下面生成exe文件

由于我的py文件需要讀這兩個配置文件,因此還需要將.exe文件和這兩個配置文件放在同一個目錄下面,就可以到任意一臺windows下面執行了

四、附上代碼filetran.py

# autor: yangbao# date: 2019-10-16import osimport timeimport datetimeimport reimport shutilimport configparserdef variable_replace(variable): '''路徑替換''' 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(time.strftime(’%Y’)) curr_month = str(time.strftime(’%m’)) curr_day = str(time.strftime(’%d’)) if re.search(’{YYYYMMDD}’, variable): variable = variable.replace(’{YYYYMMDD}’, curr_year+curr_month+curr_day) if re.search(’{YYYYMM}’, variable): variable = variable.replace(’{YYYYMM}’, curr_year+curr_month) if re.search(’{MMDD}’, variable): variable = variable.replace(’{MMDD}’, curr_month+curr_day) if re.search(’{YYYY}’, variable): variable = variable.replace(’{YYYY}’, curr_year) if re.search(’{MM}’, variable): variable = variable.replace(’{MM}’, curr_month) if re.search(’{DD}’, variable): variable = variable.replace(’{DD}’, curr_day) return variabledef source_to_target(): '''讀取settings.txt文件,將源端和目標端映射關系對上''' source_to_target_dict = {} with open(’settings.txt’, ’r’, encoding=’utf-8-sig’) as f: for line in f.readlines(): # 排除注釋和空行和格式不正確的 if not line.startswith(’#’) and line.strip() != ’’ and re.search(’=’, line): source = line.split(’=’)[0].strip() target = line.split(’=’)[1].strip() source_to_target_dict[source] = target return source_to_target_dictdef create_ok_file(source): '''讀取配置文件''' cf = configparser.ConfigParser(delimiters=(’=’)) cf.read('settings.txt', encoding=’utf-8-sig’) options = cf.options('create_ok_file') for i in options: if source.lower() == i.lower().strip(): return True return Falsedef filecopy(): '''文件拷貝''' # 得到映射表 source_to_target_dict = source_to_target() # 讀取每一個目標路徑 for ori_source, ori_target in source_to_target_dict.items(): source = variable_replace(ori_source) target = variable_replace(ori_target) # 如果源端填的是文件夾 if source.endswith(os.sep): if os.path.exists(source): file_list = os.listdir(source) for filename in file_list: # 如果目標路徑不存在,就創建 if not os.path.exists(target): os.makedirs(target) source_file = source + filename target_file = target + filename print(’[’, time.strftime(’%Y-%m-%d %H:%M:%S’), ’] ’, source_file, ’ ----> ’, target_file, ’ 開始拷貝’, sep=’’) try: shutil.copyfile(source_file, target_file) if create_ok_file(ori_source): ok_file = target_file + ’.ok’ fp = open(ok_file, ’w’) fp.close() except Exception as e: with open(error_log_name, ’a+’, encoding=’utf-8-sig’) as f: f.write(str(e)) f.write(’n’) break # print(’[’, time.strftime(’%Y-%m-%d %H:%M:%S’), ’] ’, source_file, ’ ----> ’, target_file, ’ 拷貝完成’, sep=’’) # 如果源端填的是文件 else: source_dir = source[0:source.rfind(os.sep)+1] # 得到該文件所在的文件夾 file_name_pattern = source[source.rfind(os.sep)+1:] # 得到該文件的文件樣式 if os.path.exists(source_dir): file_list = os.listdir(source_dir) for filename in file_list: # 只有匹配上的才拷貝 if re.match(file_name_pattern, filename): # 如果目標路徑不存在,就創建 if not os.path.exists(target): os.makedirs(target) source_file = source_dir + filename target_file = target + filename print(’[’, time.strftime(’%Y-%m-%d %H:%M:%S’), ’] ’, source_file, ’ ----> ’, target_file, ’ 開始拷貝’, sep=’’) try: shutil.copyfile(source_file, target_file) if create_ok_file(ori_source): ok_file = target_file + ’.ok’ fp = open(ok_file, ’w’) fp.close() except Exception as e: with open(error_log_name, ’a+’, encoding=’utf-8-sig’) as f: f.write(str(e)) f.write(’n’) break # print(’[’, time.strftime(’%Y-%m-%d %H:%M:%S’), ’] ’, source_file, ’ ----> ’, target_file, ’ 拷貝完成’, sep=’’)def warnlog(): '''警告日志''' with open(’okfile.txt’, ’r’, encoding=’utf-8’) as f: for line in f.readlines(): # 排除注釋和空行和格式不正確的 if not line.startswith(’#’) and line.strip() != ’’: okfile = variable_replace(line.strip()) if not os.path.isfile(okfile): with open(warn_log_name, ’a+’, encoding=’utf-8-sig’) as t: t.write(okfile + ’ 該文件不存在!’) t.write(’n’)if __name__ == ’__main__’: # 主程序 customer_input = input(’請輸入需要拷貝的8位指定日期,如20191114,如果不輸入,默認拷貝當天n’) # 如果沒輸入,或者輸入格式正確,就拷貝 if re.match(’d{8}’,customer_input) or not customer_input: begin_time = datetime.datetime.now() error_log_name = ’error_’ + str(time.strftime(’%Y%m%d_%H%M%S’)) + ’_.log’ warn_log_name = ’warn_’ + str(time.strftime(’%Y%m%d_%H%M%S’)) + ’_.log’ print(’[’, time.strftime(’%Y-%m-%d %H:%M:%S’), ’] ’, ’文件開始拷貝...’, sep=’’) print(’-’ * 50) filecopy() warnlog() end_time = datetime.datetime.now() cost_time = (end_time - begin_time).seconds print(’-’ * 50) print(’[’, time.strftime(’%Y-%m-%d %H:%M:%S’), ’] ’, ’文件拷貝結束,總耗時’, cost_time, ’秒’, sep=’’) # 如果輸入格式不正確 elif not re.match(’d{8}’, customer_input): print(’請輸入正確的格式’) input(’按回車鍵退出’)

settings.txt

# 拷貝路徑設置# 源端路徑不存在就不復制,目標端路徑不存在會自動創建目錄# 說明事項:# 1. 格式為源端路徑 = 目標路徑# 2. 文件夾后面以反斜杠結束# 3. 如果是變量,則以大括號闊起來,如今天是20191012, {YYYYMMDD}會替換為20191012,則使用{MMDD}替換為1012,{DD}替換為12# 4. YYYY MM DD都填大寫# 以下是示例# 拷貝整個文件夾 --> P:信息技術部YangBaooa = E:test2# 拷貝指定名稱,*表示匹配任意字符 --> D:test3{YYYYMMDD}ab* = E:test4{YYYYMMDD}[no_create_ok_file]# 將不需要生成ok標識的路徑或文件填在這下面D:test3{YYYYMMDD} = E:test4{YYYYMMDD}[create_ok_file]# 將需要生成ok標識的路徑或文件填在這下面D:test1 = E:test2

okfile.txt

# ok文件設置設置# 以下是示例# {YYYYMMDD}會替換成指定日期,D:test3{YYYYMMDD}ab.txt# D:test3{YYYYMMDD}sdfg

filetran.exe

https://pan.baidu.com/s/1vxO6UycDtz5nN4DpmjLN5w 提取碼:bgdu

注意不管是使用python去執行filetran.py,還是單擊filetran.exe,都需要跟settings.txt和okfile.txt放在一起,否則程序會報錯。

以上就是python 寫一個文件分發小程序的詳細內容,更多關于python 文件分發的資料請關注好吧啦網其它相關文章!

標簽: Python 編程
相關文章:
主站蜘蛛池模板: 91在线播放视频 | 草莓视频caomei888 | 黄网站色在线视频免费观看 | 日韩成人免费视频播放 | 久久这里只有精品国产 | 国产成人亚洲综合a∨婷婷 国产成人亚洲综合无 | 深夜免费福利视频在线播放 | 成人做爰免视频高清 | 国产在线干 | 免费可在线观看黄的视频 | 国产caoni111在线观看视频 | 久久99精品久久久久久久野外 | 欧美日本黄色片 | 亚洲精品人成网在线播放蜜芽 | 欧美大片国产在线永久播放 | 欧美日一级| 国产成人精品精品欧美 | 免费国产 | 特黄特黄aaaa级毛片免费看 | 私人影院aaaaa毛片 | 亚洲二区在线观看 | 麻豆精品在线 | 老师xxxxbbbb| 国产精品人成人免费国产 | 97视频在线免费 | 在线观看二区三区午夜 | 国产精品久草 | 日韩在线手机看片免费看 | 国产成人亚洲精品91专区手机 | 午夜一级大片 | 欧美成人一区二区三区不卡视频 | 日韩第八页 | 深夜影院在线视频观看 | 国产日韩在线看 | 二级黄绝大片中国免费视频 | 国产精品亚洲欧美日韩区 | 久久久99精品免费观看 | 欧美色五月 | 天天拍夜夜操 | 一级特黄色 | 亚洲一级毛片在线观播放 |