Unable to read downloaded file after converting python file to .exe - python-3.x

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.

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

Related

Spyder FileNotFoundError

I'm a newbie to programming. I want to switch my code from Jupyter to Spyder as I find the debugger better.
I'm trying to do something simple and open a file and I keep getting the "No such file or directory message". My text file is in the same directory as my .py file
file_palette = open(r"C:\Users\Myname.spyder-py3\palette.txt", 'r')

FileNotFoundError: [Errno 2] No such file or directory for Streamlit

I am trying to make a streamlit dashboard but just to get started, I am trying to import my csv file with data. I added the csv file into the file in which I am working with on vscode which is pictured. I also coppied the path of the csv file which is
"C:\Users\solim\AppData\Roaming\.anaconda\navigator\.anaconda\navigator\scripts\FinalProject\survey.csv"
this path matches up to the path of where my python code is that I run for streamlit but once again it keeps saying that the file cannot be found even though it is exactly where my python code is I just do not understand and it is very frustrating.
Error when I run:
Proof that the file name is spelled correct and the file is in my folder:
I am expecting the file data to be printed on my streamlit page.
screenshot of launch.json code
Enter "args": ["run", "${file}"] into j.son file
Follow the steps from this tutorial to get to launch.json. After, the file should be found

Python mammoth error: zipfile.BadZipFile: File is not a zip file

My code is ran in Python 3.8.2, name of file is main.py
After I run python main.py, i receive the error like this:
My path to file is : C:\Users\84165\Desktop\KLTN-backend\upload\thainq\Test.docx
I have searched in stackoverflow for hours about this error, but there is no solution.
Am i wrong in somewhere?
Thank you in advance.
There are two reasons this may be happening
Your docx file is completely empty (0 bytes).
The docx file your code is operating on through mammoth.convert_to_html or mammoth.extract_raw_text is currently open. Please close your file, then run the script.

How to create a Python Executable that can run other python-files stored in a folder?

Is it possible to create a Python Executable file (using PyInstaller or similar) that can in its code access other Python-files stored in a specific folder?
The reason for that it MUST be an Executable is that the script sometimes must be run from computers that has not it's own Python installed. I have no problem creating an executable (with PyInstaller for example) and they work fine. The python script itself loads various kinds of data into a database. But everytime there is a new kind of data that has to be loaded into the database I have to recreate the hole exe-file. I'm looking for a way for the executable to access python-files (with Data Load Instructions) so that the actual pyton load-files can be altered instead of the exe-file.
I have tried to find a solution using this:
import os, time
from subprocess import call
for file in os.listdir('.'):
if file == 'loadInstructions.py':
call(['python', file])
print(file)
cwd = os.getcwd()
print(cwd)
input('To EXIT progran press ENTER.')
time.sleep(3)
It works running it from a python editor but when I turn this into an exe-file it does not. If I creat an exe-file with "call(['python', file])" commented out the rest works which I interpret that the exe-file can find the file in question but not run it.
I would be gratefule for any help.

Error when Converting .py to .exe with cx_freeze

I am trying to convert my .py code to .exe. I tried all the options from posts on here and on the web, and all I could up with was the error below when I used cx_Freeze, and the .exe not opening when I used pyinstaller.
can someone help with the error below or give an alternative that works?
PS: my py code is actually ipython code from my jupyter notebook, but I'm trying not to share the code source.
Finally got it working while using pyinstaller, the key was to open the .exe file from the command window to be able to see the warning, which was "Import cv2 not found" after I deleted that line(since I was not using that module) and re built it, the .exe file now works. Hope this will help someone one day

Resources