最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

python 寫一個(gè)文件分發(fā)小程序

 更新時(shí)間:2020年12月05日 11:11:47   作者:小豹子的網(wǎng)絡(luò)記事本  
這篇文章主要介紹了python 寫一個(gè)文件分發(fā)小程序,幫助大家更好的理解和學(xué)習(xí)python,感興趣的朋友可以了解下

一、概述

該小程序?qū)崿F(xiàn)從源端到目標(biāo)端的文件一鍵拷貝,源端和目標(biāo)段都在一臺(tái)電腦上面,只是目錄不同而已

二、參數(shù)文件說(shuō)明

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

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

2. okfile.txt的說(shuō)明
okfile.txt填的源端的ok文件,有些系統(tǒng)在生成文件的時(shí)候,會(huì)生成一個(gè)ok文件,表示系統(tǒng)文件已經(jīng)生成完成。okfile.txt就是來(lái)校驗(yàn)這些文件是否存在,如果不存在,那么運(yùn)行這個(gè)小程序的時(shí)候就會(huì)生成一個(gè)warn.log,但是不影響實(shí)際的拷貝。

三、程序說(shuō)明

由于業(yè)務(wù)人員不懂python,也沒有裝開發(fā)環(huán)境,因此通過將python文件打包成一個(gè)exe的形式,方便他們操作。

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

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

四、附上代碼
filetran.py

# autor: yangbao
# date: 2019-10-16
import os
import time
import datetime
import re
import shutil
import configparser


def 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 variable


def source_to_target():
 """讀取settings.txt文件,將源端和目標(biāo)端映射關(guān)系對(duì)上"""
 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_dict


def 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 False


def filecopy():
 """文件拷貝"""

 # 得到映射表
 source_to_target_dict = source_to_target()

 # 讀取每一個(gè)目標(biāo)路徑
 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:
 # 如果目標(biāo)路徑不存在,就創(chuàng)建
 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):
 # 如果目標(biāo)路徑不存在,就創(chuàng)建
 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('請(qǐng)輸入需要拷貝的8位指定日期,如20191114,如果不輸入,默認(rèn)拷貝當(dāng)天\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'), '] ', '文件拷貝結(jié)束,總耗時(shí)', cost_time, '秒', sep='')
 # 如果輸入格式不正確
 elif not re.match('\d{8}', customer_input):
 print('請(qǐng)輸入正確的格式')
 input('按回車鍵退出')

settings.txt 

# 拷貝路徑設(shè)置
# 源端路徑不存在就不復(fù)制,目標(biāo)端路徑不存在會(huì)自動(dòng)創(chuàng)建目錄
# 說(shuō)明事項(xiàng):
# 1. 格式為源端路徑 = 目標(biāo)路徑
# 2. 文件夾后面以反斜杠結(jié)束\
# 3. 如果是變量,則以大括號(hào)闊起來(lái),如今天是20191012, {YYYYMMDD}會(huì)替換為20191012,則使用{MMDD}替換為1012,{DD}替換為12
# 4. YYYY MM DD都填大寫
# 以下是示例
# 拷貝整個(gè)文件夾 --> P:\信息技術(shù)部\YangBao\oa\ = E:\test2\
# 拷貝指定名稱,*表示匹配任意字符 --> D:\test3\{YYYYMMDD}\ab* = E:\test4\{YYYYMMDD}\

[no_create_ok_file]
# 將不需要生成ok標(biāo)識(shí)的路徑或文件填在這下面

D:\test3\{YYYYMMDD}\ = E:\test4\{YYYYMMDD}\


[create_ok_file]
# 將需要生成ok標(biāo)識(shí)的路徑或文件填在這下面

D:\test1\ = E:\test2\

okfile.txt

# ok文件設(shè)置設(shè)置
# 以下是示例
# {YYYYMMDD}會(huì)替換成指定日期,D:\test3\{YYYYMMDD}\ab.txt

# D:\test3\{YYYYMMDD}\sdfg

filetran.exe

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

注意不管是使用python去執(zhí)行filetran.py,還是單擊filetran.exe,都需要跟settings.txt和okfile.txt放在一起,否則程序會(huì)報(bào)錯(cuò)。

以上就是python 寫一個(gè)文件分發(fā)小程序的詳細(xì)內(nèi)容,更多關(guān)于python 文件分發(fā)的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!

相關(guān)文章

  • python正則表達(dá)式面試題解答

    python正則表達(dá)式面試題解答

    這篇文章主要為大家分析了python正則表達(dá)式常見面試題,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2018-01-01
  • Pytorch搭建YoloV4目標(biāo)檢測(cè)平臺(tái)實(shí)現(xiàn)源碼

    Pytorch搭建YoloV4目標(biāo)檢測(cè)平臺(tái)實(shí)現(xiàn)源碼

    這篇文章主要為大家介紹了Pytorch搭建YoloV4目標(biāo)檢測(cè)平臺(tái)實(shí)現(xiàn)源碼,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪
    2022-05-05
  • PyQt5之基礎(chǔ)框架解讀

    PyQt5之基礎(chǔ)框架解讀

    這篇文章主要介紹了PyQt5之基礎(chǔ)框架,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教
    2024-02-02
  • Python中的Super用法示例詳解

    Python中的Super用法示例詳解

    Python中可以直接通過調(diào)用父類名調(diào)用父類方法,在多重繼承中,使用super()是一個(gè)很好的習(xí)慣,下面這篇文章主要給大家介紹了關(guān)于Python中Super用法的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-04-04
  • 基于OpenCV的直方圖匹配的實(shí)現(xiàn)方法

    基于OpenCV的直方圖匹配的實(shí)現(xiàn)方法

    這篇文章主要介紹了基于OpenCV的直方圖匹配的實(shí)現(xiàn)方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2021-04-04
  • pandas將DataFrame的幾列數(shù)據(jù)合并成為一列

    pandas將DataFrame的幾列數(shù)據(jù)合并成為一列

    本文主要介紹了pandas將DataFrame的幾列數(shù)據(jù)合并成為一列,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2022-02-02
  • Python爬蟲:Request Payload和Form Data的簡(jiǎn)單區(qū)別說(shuō)明

    Python爬蟲:Request Payload和Form Data的簡(jiǎn)單區(qū)別說(shuō)明

    這篇文章主要介紹了Python爬蟲:Request Payload和Form Data的簡(jiǎn)單區(qū)別說(shuō)明,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來(lái)看看吧
    2020-04-04
  • Python使用POP3和SMTP協(xié)議收發(fā)郵件的示例代碼

    Python使用POP3和SMTP協(xié)議收發(fā)郵件的示例代碼

    這篇文章主要介紹了Python使用POP3和SMTP協(xié)議收發(fā)郵件的示例代碼,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧
    2019-04-04
  • python實(shí)現(xiàn)從ftp服務(wù)器下載文件的方法

    python實(shí)現(xiàn)從ftp服務(wù)器下載文件的方法

    這篇文章主要介紹了python實(shí)現(xiàn)從ftp服務(wù)器下載文件的方法,涉及Python操作FTP的相關(guān)技巧,非常具有實(shí)用價(jià)值,需要的朋友可以參考下
    2015-04-04
  • python 第三方庫(kù)paramiko的常用方式

    python 第三方庫(kù)paramiko的常用方式

    這篇文章主要介紹了python 第三方庫(kù)paramiko的常用方式,本文給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2021-02-02

最新評(píng)論

新竹市| 马鞍山市| 汉源县| 邛崃市| 潞城市| 治县。| 蕉岭县| 会理县| 陕西省| 德令哈市| 兰溪市| 四会市| 汝城县| 获嘉县| 上高县| 安义县| 扎鲁特旗| 汉沽区| 两当县| 鄂温| 屏东县| 纳雍县| 岑溪市| 温州市| 成安县| 民权县| 井陉县| 江北区| 淮北市| 彩票| 恩平市| 金山区| 罗城| 南阳市| 罗江县| 江山市| 黄梅县| 新宁县| 六盘水市| 体育| 界首市|