0

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()
4
  • 7
    This code is very sloppily indented. Try sticking to a standard such as two or four spaces per indent depth. Once you edit this it should greatly help with readability - us pythonistas are very finnicky! Commented Nov 21, 2013 at 9:37
  • Pep8 please :) Commented Nov 21, 2013 at 9:46
  • Do you mean you are not able to raise an exception, or to catch one? Commented Nov 21, 2013 at 9:49
  • Your code is using a mixture of tabs and spaces for indentation. Python code must use only one of the other. A Python-aware text editor can be of great assistance to coding in Python. Commented Nov 21, 2013 at 9:49

1 Answer 1

2

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

Sign up to request clarification or add additional context in comments.

1 Comment

Well this worked. Another workaround I did was using if...else in os.path.exists(source_path): sentence

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.