Python Py2app:使用easygui在硬盘上保存文件

Python Py2app:使用easygui在硬盘上保存文件,python,pandas,networkx,py2app,easygui,Python,Pandas,Networkx,Py2app,Easygui,我通常将Python脚本中创建的xls文件保存在硬盘上。例如,对熊猫来说,这通常是一件非常直截了当的事情 我的问题是,我试图通过一个py2app编译脚本来实现这一点。我尝试使用easygui询问将文件保存在哪里(哪个文件夹),但我不确定如何操作,一旦编译到应用程序中,它最终崩溃 以下是我的尝试: path = easygui.diropenbox() #Easygui is used to get a path in order to save the file to the right pla

我通常将Python脚本中创建的xls文件保存在硬盘上。例如,对熊猫来说,这通常是一件非常直截了当的事情

我的问题是,我试图通过一个py2app编译脚本来实现这一点。我尝试使用easygui询问将文件保存在哪里(哪个文件夹),但我不确定如何操作,一旦编译到应用程序中,它最终崩溃

以下是我的尝试:

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
我是否可以修改此脚本,将名为“name”的excel文件从py2app编译的应用程序保存到使用“easygui.diropenbox()”选择的文件夹中

回溯如下:

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]
回溯(最近一次呼叫最后一次):
文件“/Users/imf/Dropbox/Python/Tests/test2/myscript.py”,第135行,在
nx.write_gexf(G,路径,名称+“.gexf”)
文件“”,第2行,处于写入状态
文件“/Library/Python/2.7/site packages/networkx-1.8.1-py2.7.egg/networkx/utils/decorators.py”,第241行,在打开的文件中
fobj=_dispatch_dict[ext](路径,模式=模式)
IOError:[Errno 21]是一个目录:'/Users/Rodolphe/Desktop/chosenfolder'
[在65.5s内完成,退出代码为1]
[shell_cmd:python-u”/Users/imf/Dropbox/python/Tests/test2/myscript.py“]
[dir:/Users/Rodolphe/Dropbox/Python/Tests/test2]
[路径:/usr/bin:/bin:/usr/sbin:/sbin]

以下是一个工作版本:

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
请注意,
to_excel()
方法有一个初始参数,它是您正在编写的excel工作表的完整路径名,第二个参数是工作表的名称


此外,堆栈跟踪似乎指向脚本的另一部分,可能指向另一个bug。

是否存在崩溃的堆栈跟踪?刚刚添加了它。谢谢太好了,谢谢,非常感谢。今天晚些时候我会试试的。