Unable to read downloaded file after converting python file to .exe

Issue

I have written some code in python with the help of selenium which is doing web crawling in which it goes to a website after entering login id and password and downloaded a file. I have written the code in jupyter notebook with file name as GSPL_Code.ipynb and with the use of !jupyter nbconvert --to script GSPL_Code.ipynb and !pyinstaller GSPL_Code.py, I have first converted .ipynb file to .py and then made a .exe file from the .py file.

If I run the .exe file which is named as GSPL_Code and it is stored in dist/GSPL_Code then a file crv_report.xlsx is downloaded in the dist/GSPL_Code folder where the executable file is also there but the problem is code does not executed after downloading a file in that location. It got stuck. Nothing is happening now. When I run in jupyter notebook then I don’t face any problem but after converting it into .exe, I am facing the problem.

Please find the following python code which is part of the whole script:

    def handler(driver):
        curr=driver.current_window_handle
        for handle in driver.window_handles:
            driver.switch_to.window(handle)
            if handle != curr:
                driver.close()
        for handle in driver.window_handles:
            driver.switch_to.window(handle)
            print(driver.current_url)
        click_event('//img[@id="IconImg_crv_report_toptoolbar_export"]')    
        click_event('//div[@style="white-space:nowrap;text-overflow:ellipsis;overflow:hidden;width:249px"]')
        click_event('//span[@title="Microsoft Excel Workbook Data-only"]')
        sleep(5)
        click_event('//td[@class="wizbutton"]')   
        sleep(15)
        print("Report is downloaded")
        PATH1="C:\\Users\\ankit19.gupta\\Desktop\\Test_GSPL\\dist\\GSPL_Code"
        crv_report= "/crv_report.xlsx"
        print("Reading File")
        dataframe1 = pd.read_excel(PATH1+crv_report)
        print("Data is stored in the dataframe")
        return dataframe1

I have written some print statements to see where the error is coming. So, here I am unable to see the print statement "Report is downloaded". Before this click_event('//td[@class="wizbutton"]') is used to click a button which download the file in chrome browser and I can see this downloaded file in the path dist/GSPL_Code directory and it got executed successfully but after that it got stuck and nothing happened.

I am unable to find what is happening here. Can anyone please help me. Any help would be appreciated.

Edit: There were some delay due to which print statements "Report is not downloaded" and "Reading File" were not showing in console but now I am able to see it but it is unable to read the file crv_report which is on the path PATH1 and it got stuck here and due to which I am unable to see print statement Data is stored in the dataframe

I think code is unable to access the file because os.remove is also not working when I try to delete the crv_report file. Can anyone please help me.

Solution

OS Directory Change:

When refactoring python files, in your case changing .py to .exe, it’s important to make sure everything is a part of the current working directory. Simple OS checker command:

cwd = os.getcwd()

If directories are misaligned, you will need to run an OS command to change the directory before interacting with any files:

os.chdir('/your/desired/directory')

Answered By – Luke Hamilton

This Answer collected from stackoverflow, is licensed under cc by-sa 2.5 , cc by-sa 3.0 and cc by-sa 4.0

Leave a Reply

(*) Required, Your email will not be published