无法在代码中引发异常

时间:2013-11-21 09:35:10

标签: python exception-handling

试图解决问题但是没有运气。有些人可以告诉我这是什么问题。尝试重新缩进代码。 我无法在代码中的异常块中打印File Not found文本。 这是缩进问题吗?

代码段:

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()

1 个答案:

答案 0 :(得分:2)

尝试一些优秀的编辑器,例如pyscripterEmacs,让您的pythonic生活变得轻松:)

我试图打算你的代码......

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 Not Found ..'

,应该在

的else循环中
  

os.path.exists(SOURCE_PATH):

检查并打印源路径不存在