python-3.x - How to change python script to .exe with user defined input and output paths in python
问题描述
I have python script , i want to change this simple script to .exe file with user defined input and output path .
in below script 'csv' is input folder and contain multiple txt files ,
import pandas as pd
import numpy as np
import os
for file in os.listdir('csv/'):
filename = 'csv/{}'.format(file)
print(filename)
df=pd.read_csv(filename)
df.to_csv(path_out)
解决方案
使用 cx_freeze 执行此操作的简单方法如下:
conda install -c conda-forge cx_freeze,或 pip install cx_freeze 使用 numpy 和 pandas 到您的环境
为您的新 .exe 创建一个名为 dist 的文件夹
将下面的代码另存为 csv_thing.py,或者您希望调用它的任何内容。
运行命令:cxfreeze csv_thing.py --target-dir C:\somepath\dist
如果不使用 cx_freeze 设置文件(pyinstaller 中的规范文件),很有可能并非所有文件都会被复制到 dist 目录。Anaconda 环境中的 Numpy 和 pandas 通常很棘手。
如果出现文件故障,您可以手动将 .dll 文件复制到 dist 文件夹中;如果你把它们都抓起来就很容易了。如果您使用的是 Anaconda 环境,他们可能会住在这里:C:\Users\your_user_account\Anaconda3\envs\panel\Library\bin。否则从 numpy 位置获取所有这些:C:\Users\matth\Anaconda3\envs\panel\Lib\site-packages\numpy 并复制到 dist 目录。
import numpy as np
import pandas as pd
import os
in_dir = input(' enter a folder path where your .csvs are located: ')
out_dir = input(' enter a folder path where your .csvs will go: ')
csv_list = [os.path.join(in_dir, fn) for fn in next(os.walk(in_dir))[2]if fn.endswith('.csv')]
for csv in csv_list:
file_name = os.path.basename(csv)
print(file_name)
df = pd.read_csv(csv)
df.to_csv(os.path.join(out_dir, file_name))
推荐阅读
- python - 列表理解有些混乱
- angular - 在 ionic 4 项目中使用 OneSignal Api
- python - 如何将计算应用于文本文件的列?
- javascript - 如果文本相同,是否有办法停止功能,如果文本不同,则重新运行该功能?
- python - 每个名称有两个值的散点图
- swift - 在结构中过滤结构数组
- reactjs - 为什么尝试导入时无法识别我的组件?
- django - Django ORM - 模型引用另一个模型ManyToMany字段
- excel - 使用 VBA 添加超链接以创建说明选项卡
- mysql - MySQL 对分组 (GROUP BY) 结果使用 ORDER BY