Pratyush Kulwal
Pratyush Kulwal

Reputation: 165

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

Upvotes: 0

Views: 128

Answers (1)

Shashi
Shashi

Reputation: 2175

Try some good editors like pyscripter ,Emacs to make your pythonic life easy :)

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

, should be in else loop of

os.path.exists(source_path):

to check and print source path is not exists

Upvotes: 2

Related Questions