【发布时间】:2014-03-17 13:34:04
【问题描述】:
以下代码可自行运行。我用 py2exe 创建了一个无法运行的可执行文件。该脚本处理和排序一些 csv 文件。如果我将此代码放在运行可执行文件的“dist”目录中(因此它与正在处理的 csv 文件具有相同的文件路径深度),则代码有效;但是同一目录中的可执行文件本身不起作用。
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")
原始输入语句也没有保持屏幕打开,所以我看不到发生了什么。
谢谢
【问题讨论】:
-
你真的不应该尝试在
py2exe可执行文件中访问__file__之外的东西。您是否有理由不使用data_files,或者更好的是setuptools及其pkgresources? -
附带说明,在路径名上调用
str.split是个坏主意。您已经在使用os.path进行所有其他路径操作;打电话给dirname(dirname(current_dir))或类似的有什么问题? -
我不使用你建议的东西的原因是我是一个 python n00b。 :-) 我在其他线程中发现
_file_可能是问题所在。我会为 py2exe 设置脚本使用 data_files,对吧?
标签: python path executable py2exe