以下代码自行运行。我用py2exe创建了一个不可用的可执行文件。该脚本处理和排序一些CSV文件。如果我把这段代码放在运行可执行文件的“dist”目录下(因此它与正在处理的csv文件具有相同的文件路径深度),代码就可以工作;但同一目录中的可执行文件本身不起作用。python代码工作,但不能在p2exe可执行文件中
import glob
import os
import pandas as pd
current_dir = os.path.dirname(os.path.realpath(__file__))
directory = os.path.sep.join(current_dir.split(os.path.sep)[:-2])
csvfiles = os.path.join(directory, '*.csv')
for csvfile in glob.glob(csvfiles):
filename = os.path.basename(csvfile)
if '_sorted' in filename:
print "Remove this file"
os.remove(csvfile)
csvfiles = os.path.join(directory, '*.csv')
for csvfile in glob.glob(csvfiles):
filename = csvfile
df = pd.read_csv(filename)
df = df[df["ORGANIZATION"]!="WPPL"]
df = df.sort('MEETING START TIME')
#write new csv file
df.to_csv(filename + '_sorted.csv', cols=["DATE","MEETING START TIME","MEETING END TIME","DESCRIPTION","ORGANIZATION","LOCATION"],index=False)
raw_input("Press enter to close")
此外原始输入语句不保持屏幕打开,所以我不能真正看到发生了什么。
感谢
你真的不应该尝试从'py_exe''可执行文件中的'__file__'访问东西。有没有理由不使用['data_files'](http://www.py2exe.org/index.cgi/data_files),更好的方法是使用'setuptools'及其'pkgresources'? – abarnert
作为一个便笺,调用路径名上的'str.split'是一个坏主意。您已经为所有其他路径操作使用'os.path';调用'dirname(dirname(current_dir))'或类似命令有什么问题? – abarnert
我不使用你建议的东西的原因是我是一个python n00b。 :-) 我在其他线程中发现'_file_'可能是问题。我会为py2exe安装脚本使用data_files,对吧? – mattrweaver