简体   繁体   English

无法在代码中引发异常

[英]Not able to raise exception in the code

Trying to resolve the issue But no luck yet. 试图解决问题但没有运气。 Can some body tell me what's the issue. 有些人可以告诉我这是什么问题。 Tried re indenting the code. 尝试重新缩进代码。 I am not able to print the File Not found text in the exception block in the code. 我无法在代码中的异常块中打印File Not found文本。 Is this the indentation issue ? 这是缩进问题吗?

Code Snippet: 代码片段:

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

Try some good editors like pyscripter , Emacs to make your pythonic life easy :) 尝试一些优秀的编辑器,如pyscripterEmacs ,让你的pythonic生活变得轻松:)

I have tried to intend your code ... 我试图打算你的代码......

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

Line: 线:

print'File Not Found ..' print'File Not Found ..'

, should be in else loop of ,应该在其他循环中

os.path.exists(source_path): os.path.exists(SOURCE_PATH):

to check and print source path is not exists 检查和打印源路径不存在

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM