首页 > 解决方案 > 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)

标签: python-3.xpandasnumpyexe

解决方案


使用 cx_freeze 执行此操作的简单方法如下:

  1. conda install -c conda-forge cx_freeze,或 pip install cx_freeze 使用 numpy 和 pandas 到您的环境

  2. 为您的新 .exe 创建一个名为 dist 的文件夹

  3. 将下面的代码另存为 csv_thing.py,或者您希望调用它的任何内容。

  4. 运行命令:cxfreeze csv_thing.py --target-dir C:\somepath\dist

  5. 如果不使用 cx_freeze 设置文件(pyinstaller 中的规范文件),很有可能并非所有文件都会被复制到 dist 目录。Anaconda 环境中的 Numpy 和 pandas 通常很棘手。

  6. 如果出现文件故障,您可以手动将 .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))

推荐阅读