Py2app: saving files on hard drive with easygui

1.3k views Asked by At

I usually save xls files created within Python scripts on my hard drive. That 's usually a pretty straight forward thing to do with pandas, for instance.

My problem is that I'm trying to accomplish this from a py2app-compiled script. I tried using easygui to ask where (which folder) to save the file, but I'm not sure how to go about doing it, and once compiled into an app, it crashes at the end.

Here's what I attempted:

path = easygui.diropenbox() #Easygui is used to get a path in order to save the file to the right place
dfA = pd.DataFrame(A) #the pandas dataframe
C = ['Gen','Density','ASPL','Modularity'] # pandas' excel file header
name = str(n) + "_" + str(NGEN) + "_" + str(nbrhof) + ".xls" # the name of the file (should I add the path here somewhere?)
dfA.to_excel(name, path, header=C,index=False) # exporting the dataframe to excel

Can I modify this script to save the excel file named "name" to the folder chosen with "easygui.diropenbox()", from a py2app-compiled app?

The Traceback is as follow:

Traceback (most recent call last):
File "/Users/myself/Dropbox/Python/Tests/test2/myscript.py", line 135, in <module>
nx.write_gexf(G, path, name+".gexf")
File "<string>", line 2, in write_gexf
File "/Library/Python/2.7/site-packages/networkx-1.8.1-py2.7.egg/networkx/utils/decorators.py", line 241, in _open_file
fobj = _dispatch_dict[ext](path, mode=mode)
IOError: [Errno 21] Is a directory: '/Users/Rodolphe/Desktop/chosenfolder'
[Finished in 65.5s with exit code 1]
[shell_cmd: python -u "/Users/myself/Dropbox/Python/Tests/test2/myscript.py"]
[dir: /Users/Rodolphe/Dropbox/Python/Tests/test2]
[path: /usr/bin:/bin:/usr/sbin:/sbin]
1

There are 1 answers

0
daedalus On

Here is a working version:

import pandas as pd
import easygui
import os

A = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
     'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
dfA = pd.DataFrame(A) #the pandas dataframe

path = easygui.diropenbox() # easygui is used to get a path
name = "tmp.xlsx" # the name of the file
path = os.path.join(path, name)  # this is the full pathname
dfA.to_excel(path, 'tmp_sheet') # exporting the dataframe to excel

Note that the to_excel() method has an initial parameter that is the full path name to the Excel sheet you are writing and the second parameter is the name of the worksheet.

Also it seems your stack trace is referring to another part of your script and may be pointing to another bug.