其他分享
首页 > 其他分享> > 生成pcf文件

生成pcf文件

作者:互联网

import os
import datetime
import hashlib


def checksum(filename):
    with open(filename, mode='rb') as f:
        d = hashlib.md5()
        while True:
            buf = f.read(128)
            if not buf:
                break
            d.update(buf)
        return d.hexdigest()

if __name__ == "__main__":

    week = int(datetime.date.today().strftime("%w"))

    if week in {6, 0}:
        print(week)

    nextdate = datetime.date.today() + datetime.timedelta(days=1)
    if week == 5:
        nextdate = datetime.date.today() + datetime.timedelta(days=3)

    PreTradingDay = datetime.date.today().strftime('%Y%m%d')
    TradingDay = nextdate.strftime('%Y%m%d')

    #PreTradingDay = '20191030'
    #TradingDay = '20191031'

    print(PreTradingDay)
    print(TradingDay)

    path = 'D:/data/pcf'
    template = 'fm006etfd20191104001.txt'
    pcftemp = os.path.join(path, template)

    template = 'fm006etfd20191104001.flg'
    flagtemp = os.path.join(path, template)

    pcfname = 'fm006etfd' + TradingDay +'001.txt'
    flagname = 'fm006etfd' + TradingDay + '001.flg'

    pcffile = os.path.join(path, pcfname)
    flagfile = os.path.join(path, flagname)

    print(pcffile)
    print(flagfile)


    f = open(pcftemp, mode='r', encoding='GBK',newline='\n')
    f_new = open(pcffile, mode="w+", encoding='GBK',newline='\n')

    for line in f:
        if '20191104' in line:
            line = line.replace('20191104', TradingDay)
            line = line.replace('20191101', PreTradingDay)
        f_new.write(line)
        f_new.flush()
    f.close()
    f_new.close()

    stat = os.stat(pcffile)
    FileName = pcfname
    FileDate = str(TradingDay)
    FileTime = str(datetime.datetime.fromtimestamp(stat.st_mtime).strftime("%H%M%S"))
    FileBytes = str(stat.st_size)
    CheckSum = checksum(pcffile)

    f = open(flagtemp, mode='r', encoding='GBK',newline='\n')
    f_new = open(flagfile, mode="w+", encoding='GBK',newline='\n')

    for line in f:
        if 'fm006etfd20191104001' in line:
            line = line.replace('fm006etfd20191104001.txt', FileName)
            line = line.replace('2945', FileBytes)
            line = line.replace('20191104', FileDate)
            line = line.replace('210000', FileTime)
            line = line.replace('5362833CC3437FEC33E69D0FD0287176', CheckSum)
            print(line)

        f_new.write(line)
        f_new.flush()
    f.close()
    f_new.close()
    

  

标签:文件,datetime,replace,TradingDay,new,path,line,生成,pcf
来源: https://www.cnblogs.com/timlong/p/11850920.html