2011-10-25 66 views
-2

我在文件夾中有一些zip文件。我有一個腳本來處理它們。即要寫入到數據庫中的數據是在不同的文件中,其結構如下:Python - 文件管理和處理多個zip文件

some_text;database;file_name 
some_text2;database2;file_name2 
.... 

什麼是處理這個文件的最好方法?另外,如果該文件中沒有匹配的zip文件名,則應該報告錯誤消息。

我當前的代碼:

filelist = glob.glob(os.path.join(rootdir, '*.zip')) 
if filelist: 
    for file in filelist: 
     print "Working on file ", file 
     #get only file name without .zip for compare 
     aa = file.split(sl) 
     bb = aa[len(aa) -1] 
     cc = bb.split(".") 
     ime_sole = cc[0] 

     fle = codecs.open(rootdir + sl + 'portal_schools.txt', 
          'r',encoding="cp1250") 
     line = fle.readline() 

     # Read lines 
     for line in iter(fle): 
      #print line, 
      a,b,c = line.split(";") 
      if c == ime_sole: 
       print c 
       database = str(b) 
       #distdir = str(c) 
      else: 
       print "some text" 
       return 


     fle.close() 

但這種失敗,因爲它被逐行讀取。如果在第一行沒有匹配,則代碼停止。我需要它繼續通過文件,然後,畢竟完成後,從一個新的zip文件開始。

+0

如果提高格式化你將有得到答案的一個更好的機會。 – codeape

+1

從else條件中刪除返回。代碼因此而停止。 – spicavigo

+0

我很抱歉,但是您的代碼存在很多問題:您不需要「如果filelist:」,因爲您只是迭代列表,如果列表爲空,將會很好。你也應該使用os.path.splitext()來獲取文件名。您應該從文件處理循環等中讀取數據文件。 –

回答

0

我知道我的代碼遠非完美。問題出在else。我把它移到了整個代碼的最後。這是一個新手的錯誤。我也插入了try-catch,所以如果它在一個zip文件上失敗了,下一個仍然被處理。現在,它看起來是這樣的:

filelist = glob.glob(os.path.join(rootdir, '*.zip')) 
if filelist: 
    for file in filelist: 
     try: 
      aa = file.split(sl) 
      #print "aa ",aa 
      bb = aa[len(aa) -1] 
      #print "bb ", bb 
      cc = bb.split(".") 
      #print "cc ", cc 
      ime_sole = cc[0] 
      #print "imesole ", ime_sole 

    fle = codecs.open(rootdir + sl + 'portal_schools.txt','r',encoding="cp1250") 
      #line = fle.readline() 

      data = [] 

      for line in iter(fle): 
       line = line.replace("\r\n", "") 
       x = line.split(";") 
       data.append(x) 

      result = [element for element in data if element[2] == ime_sole] 
      fle.close() 
      #print result 


      if result: 
       database = result[0][1] 

       vnos_data = "Podatki za %s , se vpisujejo v bazo %s " % (ime_sole, database) 


       host ="####" 
       user="####" 
       password = "####" 


       iUrnik_tables = iUrnik_tables_fromzip.Tables(defdir,file,sl,host,database,user,password) 

       id_skripte =iUrnik_tables[0] 
       date_begin = iUrnik_tables[1] 
       date_end = iUrnik_tables[2] 
       iUrnik_all_fromzip.FileWork(defdir,file,sl,host,database,user,password) 


       iUrnik_itt_zip.Proces(defdir,file,sl,host,database,user,password,id_skripte,date_begin,date_end) 


       trenutek = datetime.datetime.now() 
       trenutek = trenutek.strftime("%Y%m%d%H%M") 

       newfilename = os.path.splitext(file)[0] 
       newfilename = newfilename +"_" + str(trenutek) + os.path.splitext(file)[1] 


       folder = defdir + sl + ime_sole + sl + "archive" 

       destination = folder + sl 

       novoimezipa= destination + newfilename.split(sl)[-1] 

       if not os.path.exists(folder): 
        os.makedirs(folder) 
        os.chdir(folder) 


       shutil.copy(file,destination) 

       old = destination + file.split(sl)[-1] 
       os.rename(old , novoimezipa) 


       os.remove(file) 



      else: 
       nothing :) 

       #return 
     except: 
      print sys.exc_info() 


else: 
    vnos_nodata= u"V mapi %s ni podatkov za prenos" % (rootdir) 
    Logging(defdir, sl, vnos_nodata) 

我知道這是不是完美的,但它的工作原理:)

+0

祝賀解決方案。如果可以,請將您的答案標記爲「已接受」,以便其他人可以從您的成功中學習。乾杯〜 –