I have written the following python code in the jupyter notebook as:
import pandas as pd
import time
from datetime import datetime
import os
PATH="C:\\Users\\ankit19.gupta\\Desktop\\test1"
data = [['Ankit', 28], ['Akshat', 28]]
data = pd.DataFrame(data, columns=['Name', 'Age'])
os.chdir("C://Users//ankit19.gupta//Desktop//test1")
print(os.getcwd())
PATH="C:\\Users\\ankit19.gupta\\Desktop\\test1"
xlsx_file1 = str(datetime.now().strftime('%d_%m_%Y_')) + 'Test' + '.xlsx'
out_path1 = PATH+ "/Generated_Data/"+xlsx_file1
print("Writing excel file") # Till now code is executed successfully
writer1 = pd.ExcelWriter(out_path1, engine='xlsxwriter')
print("Used ExcelWriter") # Don't get this print statement
data.to_excel(writer1,index=False)
print("File is written")
writer1.save()
print("Completed")
I have created a pandas dataframe and store the data of this dataframe in an excel file and writing it on the disk. It executed successfully when I run it on jupyter notebook but when I convert it into an .py file using !jupyter nbconvert --to script test1.ipynb
and get .exe file from .py file using !pyinstaller test1.py
then when I open the .exe file from the directory \test1\dist\test1
then console gets open and show the print statement as Writing excel file
and then console gets closed and I don't get the statement Used ExcelWriter
from the print() function.
I don't know why it is happening while using executable file though I have changed the current working directory. Can anyone please help me ? Any help would be appreciated.
You ran your code on Windows, then os.chdir
with linux template is wrong. Otherwise, your destination directory must be existed before you write to
import pandas as pd
import time
from datetime import datetime
import os
PATH="C:\\Users\\ankit19.gupta\\Desktop\\test1"
data = [['Ankit', 28], ['Akshat', 28]]
data = pd.DataFrame(data, columns=['Name', 'Age'])
os.chdir("C:\\Users\\ankit19.gupta\\Desktop\\test1")
print(os.getcwd())
xlsx_file1 = str(datetime.now().strftime('%d_%m_%Y_')) + 'Test' + '.xlsx'
out_path1 = PATH+ "\\Generated_Data\\"
# make sure path existed
os.makedirs(out_path1, exist_ok = True)
out_file = PATH+ "\\Generated_Data\\" + xlsx_file1
print("Writing excel file") # Till now code is executed successfully
writer1 = pd.ExcelWriter(out_file, engine='xlsxwriter')
print("Used ExcelWriter") # Don't get this print statement
data.to_excel(writer1,index=False)
print("File is written")
writer1.save()
print("Completed")