我正在尝试为我的 jupyter notebook 代码创建一个 executable(.exe) python 文件。该代码基本上是从文件夹 A 和文件夹 B 中读取一堆文件,并找出文件夹中文件之间的差异,将结果制作成 csv。我在哪里寻找如何设置配置文件,executable 文件读取该配置文件以获取需要比较的输入文件夹(包含所有文件)的路径。此配置文件可以是 json 或用户编辑的文本文件,并为他添加当前目录,其中包含文件的两个文件夹所在的目录。在我的代码中,我从我自己的路径读取文件夹并将路径添加到 directory_A 和 directory_B 作为 directory_A = r"C:\Users\Bilal\Python\Task1\OlderVersionFiles\" 和 directory_B=r"C:\ 我知道如何将 jupyter notebook 转换为 python executable 感谢:https://stackoverflow.com/questions/55741607/is-it-possible-to-generate-an-executable-exe-of-a-jupyter-notebook This creates a build 包含大量文件的文件夹和一个应用程序文件,在我的情况下没有任何作用。Users\Bilal\Python\Task1\NewVersionFiles\”。
- 我怎样才能让它创建我的代码在单击 executable 文件时通过 jupyter 本身运行它时生成的 Record.csv 文件?使用 python 文件中的静态路径代码引用存储在我的系统中的文件夹路径。
- 我怎样才能拥有一个从配置文件读取路径并输出文件夹之间差异的 csv 的应用程序文件?
我找不同的代码如下
import os
import csv
import pandas as pd
import io
import re
dir_A_dict = dict()
directory_A = r"C:\\Users\\Bilal\\Python\\Task1\\OlderVersionFiles\\"
dir_A_files= [os.path.join(directory_A, x) for x in os.listdir(directory_A) if '.csv' in str(x)]
dir_B_dict = dict()
directory_B = r"C:\\Users\\Bilal\\Python\\Task1\\NewVersionFiles\\"
dir_B_files = [os.path.join(directory_B, x) for x in os.listdir(directory_B) if '.csv' in str(x)]
for file_ in dir_A_files:
f = open(file_, 'r')
reader = csv.reader(f)
header = next(reader)
for line in reader:
if ''.join(line) not in dir_A_dict.keys():
dir_A_dict[''.join(line)] = {
"record": line,
"file_name": os.path.basename(file_),
"folder" : "OlderVersion",
"row": reader.line_num
}
for file_ in dir_B_files:
f = open(file_, 'r')
reader = csv.reader(f)
header = next(reader)
for line in reader:
if ''.join(line) not in dir_B_dict.keys():
dir_B_dict[''.join(line)] = {
"record": line,
"file_name": os.path.basename(file_),
"folder" : "NewVersion",
"row": reader.line_num
}
aset = set()
for v in dir_A_dict.values():
aset.add(tuple(v['record']))
bset = set()
for v in dir_B_dict.values():
bset.add(tuple(v['record']))
in_a_not_b = aset - bset
in_b_not_a = bset - aset
diff = in_a_not_b.union(in_b_not_a)
record_ = []
for val in diff:
file_ = ''.join(val)
record_.append(file_)
# Writing dictionary values to a text file
with open("Report2.txt", 'w') as f:
for i in range(73488):
if record_[i] not in dir_A_dict.keys():
f.write('%s\n' % ', '.join(str(x)for x in dir_B_dict[record_[i]].values()))
else:
f.write('%s\n' % ', '.join(str(x)for x in dir_A_dict[record_[i]].values()))
# regular expression to capture contents of balanced brackets
location_regex = re.compile(r'\[([^\[\]]+)\]')
with open(r"C:\\Users\\Bilal\\Report2.txt", 'r') as fi:
# replaced brackets with quotes, pipe into file-like object
fo = io.StringIO()
fo.writelines(str(re.sub(location_regex, r'"\1"', line)) for line in fi)
# rewind file to the beginning
fo.seek(0)
# read transformed CSV into data frame
df = pd.read_csv(fo)
df.columns = ['Record', 'Filename', 'Folder', "Row"]
# print(df)
df.to_csv('Records2Arranged.csv')
回答1
没有必要让你的生活更难尝试翻译 .ibpnb
因为它只是 python 代码和一些额外的数据。
要从 python 代码创建 executable,您可以使用 http://www.py2exe.org/ 制作单个 exe 或使用带有嵌入选项的 https://stackoverflow.com/a/22513682/12141949 ,我个人觉得它更容易使用,但更难直接投入使用。