python 解压文件 重名 您所在的位置:网站首页 解压tar包并重命名文件夹 python 解压文件 重名

python 解压文件 重名

2023-08-06 19:07| 来源: 网络整理| 查看: 265

1 #!/usr/local/bin/python

2 #coding=UTF-8

3

4 importos5 importcsv as csv6 importre7 importshutil8 importos.path9 importdatetime10 importtime12 importgzip13

14

15 csv.field_size_limit(1000000)16

17

18

19

20

21

22 #输入:

23 #timestr:要创建的数据文件的时间

24 #granulityPeriods:时间粒度

25 #oridir:原始csv文件的存放目录

26 # 文件名为PM201310271604+080024A20131027.1545+0800-20131027.1600+0800_101_Carrier_-_1.xml

27 #outputdir:修改后csv文件的输出目录,格式示例为'D:/OMC_DATA/HW/GSM/OMC1/TIL-HBSC-020/PM/DECODE/'

28

29 defztetd15data(timestr,granulityPeriods,oridir,outputdir):30

31 outpath =outputdir.strip()32

33 if notos.path.exists(outpath):34 os.makedirs(outpath)35

36

37

38 createday = datetime.datetime.strptime(timestr, '%Y-%m-%d %H:%M:%S')39 #createtime

40 createtime = createday.strftime('%Y%m%d%H%M')41 delta = datetime.timedelta(minutes=granulityPeriods)42 #createtime

43 #文件名开始时间

44 starttime = createday.strftime('%Y%m%d.%H%M')45 #csv文件中collecttime:201204211100

46

47

48 createday = createday +delta49

50 #文件名中endtime

51

52 endtime = createday.strftime('%Y%m%d.%H%M')53 #csv文件名中endtime

54 modifycollecttime = createday.strftime('%Y%m%d%H%M')55

56 newfilenametime = starttime+"+0800-"+endtime+"+0800"

57

58

59

60 find_file=re.compile(r".xml$")61 for root ,dirs,files inos.walk(oridir):62

63 for file infiles:64 iffind_file.search(file):65 filename = "%s"%(file)66 inputpath = "%s"%(root+'/'+filename)67 #inputpath = oridir+'/'+filename

68 printinputpath69 #wangfanfan

70 printfilename71 fileprefix=filename[0:22]72

73 filesuffix=filename[59:-4]74 printfileprefix75 printfilesuffix76 #wangfanfan

77

78 outfile = fileprefix+newfilenametime +filesuffix79 printoutfile80 outputfile = outputdir+"/"+outfile+".xml"

81 printoutputfile

#复制文件82 shutil.copyfile(inputpath, outputfile)83

84          #将原始xml文件压缩后删除

85 f_in = open(outputfile,'rb')86 targetname=outputfile+".gz"

87 f_out = gzip.open(targetname,'wb')88 f_out.writelines(f_in)89 f_out.close()90 f_in.close()91

92 os.remove(outputfile)93

94

95

96

97

98

99

100

101 #读取配置的csv文件

102 defreadpath(oripath):103

104 pathlist =[]105

106 fp = open(oripath,'r')107 for line infp:108 printline109 line = line.strip('\n')110 paths =[]111 paths = line.split(',')112 pathdic ={}113 pathdic["ori"] =paths[0]114 pathdic["target"] = paths[1]115 if not os.path.exists(paths[1]):116 os.makedirs(paths[1])117 pathlist.append(pathdic)118

119 returnpathlist120

121

122

123

124

125

126

127

128

129

130 tdoripath = "/tomcat/***/CREATE_DATA/omcdatapathTDPM.csv"

131

132

133

134 defrunBySystemTime():135

136

137 start =time.clock()138

139 nowtime = time.strftime("%Y-%m-%d %H:%M:%S")140 timestr =nowtime141

142 #gsmpathlist = readpath(tdoripath)

143 tdpathlist =readpath(tdoripath)144

145 granulityPeriods = 15

146

147 for j inrange(len(tdpathlist)):148 path ={}149 path =tdpathlist[j]150 ztetd15data(timestr,granulityPeriods,path.get("ori"),path.get("target"))151

152

153

154 finish =time.clock()155

156 print "finished,couse:"

157 print (finish-start)158 #time.sleep(60*60)

159

160

161

162 defrunByDuration(begintime,endtime,durantion):163

164

165 printbegintime166

167 while begintime



【本文地址】

公司简介

联系我们

今日新闻

    推荐新闻

    专题文章
      CopyRight 2018-2019 实验室设备网 版权所有