2013-11-21 89 views
0

試圖解決問題但還沒有運氣。有人能告訴我什麼是問題。試圖重新縮進代碼。 我無法在代碼中的異常塊中打印文件未找到的文本。 這是縮進問題嗎?無法在代碼中引發異常

代碼段:

from xlutils.copy import copy 
from xlrd import open_workbook 
import xlwt 
import os 
import shutil 
import glob 
def openexcel_main(): 
    book = open_workbook('input.xls',formatting_info=True) 
    sheet = book.sheet_by_index(0) 
    wb = copy(book) 
    w_sheet = wb.get_sheet(0) 
    folder_name=['do_not_delete','internal_builds'] 
    for j in range (0,2): 
     folder=folder_name.pop()  
     for i in range (1,(sheet.nrows)): 
     cell_test_group = sheet.cell(i,0) 
    data=str(cell_test_group.value) 
    print '#####################################' 
    print data 
    list=[] 
    source_path='/mnt/'+folder+'/pybuild/'+data+'/MAIN/' 
     if os.path.exists(source_path): 
     try: 
     os.chdir(source_path) 
      all_subdirs = [d for d in os.listdir('.') if os.path.isdir(d)] 
      for dirs in all_subdirs: 
       dir = os.path.join('/mnt/'+folder+'/pybuild/'+data+'/MAIN/', dirs) 
       os.chdir(dir) 
       current = os.getcwd() 
       new = str(current).split("/")[6] 
       list.append(new) 
     list.sort() 
     val=list 
      for i in range (1,4): 
      if val==[]: 
       break 
      else: 
      print i 
       current_build_number=val.pop() 
      print 'Current_Build:'+current_build_number 
      source_path_copy = r""+ source_path+"/"+current_build_number+"/" 
       print 'Copying From:'+ source_path_copy 
      dest_path = r"/home/builds_repo/"+folder+"/pybuild/"+data+"/MAIN/"+current_build_number+"/" 
       os.chdir(source_path_copy) 
       file_name=(glob.glob('*[_bin].*')).pop() 
      print 'File_Copied:'+ file_name 
       if not os.path.exists(dest_path): 
        os.makedirs(dest_path) 

       shutil.copyfile(source_path_copy + file_name, dest_path + file_name) 
     except: 
      print'File Not Found ..' 
     raise 
def main(): 
    openexcel_main() 



main() 
+7

此代碼非常滑落地縮進。嘗試堅持每個縮進深度兩個或四個空格的標準。 一旦你編輯它,它應該大大有助於可讀性 - 我們pythonistas是非常finnicky! –

+0

[Pep8](http://www.python.org/dev/peps/pep-0008/)please :) – mitnk

+0

你的意思是你不能*引發異常或者引發異常? –

回答

2

嘗試一些很好的編輯器,如pyscripterEmacs,讓您的生活Python的輕鬆:)

我試圖想要你的代碼...

from xlutils.copy import copy 
from xlrd import open_workbook 
import xlwt 
import os 
import shutil 
import glob 

def openexcel_main(): 
    book = open_workbook('input.xls',formatting_info=True) 
    sheet = book.sheet_by_index(0) 
    wb = copy(book) 
    w_sheet = wb.get_sheet(0) 
    folder_name=['do_not_delete','internal_builds'] 
    for j in range (0,2): 
     folder=folder_name.pop() 
     for i in range (1,(sheet.nrows)): 
      cell_test_group = sheet.cell(i,0) 
      data=str(cell_test_group.value) 
      print '#####################################' 
      print data 
    list=[] 
    source_path='/mnt/'+folder+'/pybuild/'+data+'/MAIN/' 
    if os.path.exists(source_path): 
     try: 
      os.chdir(source_path) 
      all_subdirs = [d for d in os.listdir('.') if os.path.isdir(d)] 
      for dirs in all_subdirs: 
       dir = os.path.join('/mnt/'+folder+'/pybuild/'+data+'/MAIN/', dirs) 
      os.chdir(dir) 
      current = os.getcwd() 
      new = str(current).split("/")[6] 
      list.append(new) 
      list.sort() 
      val=list 
      for i in range (1,4): 
       if val==[]: 
        break 
       else: 
        print i 
       current_build_number=val.pop() 
       print 'Current_Build:'+current_build_number 
       source_path_copy = r""+ source_path+"/"+current_build_number+"/" 
       print 'Copying From:'+ source_path_copy 
       dest_path = r"/home/builds_repo/"+folder+"/pybuild/"+data+"/MAIN/"+current_build_number+"/" 
       os.chdir(source_path_copy) 
       file_name=(glob.glob('*[_bin].*')).pop() 
       print 'File_Copied:'+ file_name 
       if not os.path.exists(dest_path): 
        os.makedirs(dest_path) 

       shutil.copyfile(source_path_copy + file_name, dest_path + file_name) 
     except Exception ,e: #Use Exception if not sure which exception will raise 
      print'File Not Found ..',e 
      #raise 
def main(): 
    openexcel_main() 


if __name__ == '__main__': #Use main 
    main() 

行:

個print'File未找到..」

,應在

os.path.exists(SOURCE_PATH)否則循環:

檢查和打印源路徑是不存在

+0

那麼這工作。我做的另一個解決方法是在os.path.exists(source_path)中使用if ... else:sentence –

+0

感謝IDE –