如何从文本文件上下文菜单中 运行 python 脚本并将其与其他文本文件进行比较?
How to run a python script from a text file context menu and compare it to other text files?
我写了一个 python 脚本,它将两个文件作为输入,然后将它们之间的差异作为输出保存在另一个文件中。
我将它绑定到一个批处理文件 .cmd
(见下文)并将批处理文件添加到文本文件的上下文菜单中,所以当我右键单击一个文本文件并 select 它, cmd
window 弹出,我输入要比较的文件地址。
批处理文件内容:
@echo off
cls
python "C:\Users\User\Desktop\Difference of Two Files.py" %1
Python代码:
import sys
import os
f1 = open(sys.argv[1], 'r')
f1_name = str(os.path.basename(f1.name)).rsplit('.')[0]
f2_path = input('Enter the path of file to compare: ')
f2 = open(f2_path, 'r')
f2_name = str(os.path.basename(f2.name)).rsplit('.')[0]
f3 = open(f'{f1_name} - {f2_name} diff.txt', 'w')
file1 = set(f1.read().splitlines())
file2 = set(f2.read().splitlines())
difference = file1.difference(file2)
for i in difference:
f3.write(i + '\n')
f1.close()
f2.close()
f3.close()
现在,我的问题是如何用接受多个文件的拖放解决方案替换第二个文件路径的输入。
我对 python 代码没有任何问题,可以自己扩展它以包含更多文件。我只是不知道如何编辑批处理文件,所以不是通过键入路径只获取一个文件,而是通过拖放来获取多个文件。
非常感谢你的帮助。
终于,我自己想通了!
我 Post 最后的代码,也许它对某人有帮助。
# This script prints those lines in the 1st file that are not in the other added files
# and saves the results into a 3rd file on Desktop.
import sys
import os
f1 = open(sys.argv[1], 'r')
f1_name = str(os.path.basename(f1.name)).rsplit('.')[0]
reference_set = set(f1.read().splitlines())
compare_files = input('Drag and drop files into this window to compare: ')
compare_files = compare_files.strip('"').rstrip('"')
compare_files_list = compare_files.split('\"\"')
compare_set = set()
for file in compare_files_list:
with open(os.path.abspath(file), 'r') as f2:
file_content = set(f2.read().splitlines())
compare_set.update(file_content)
f3 = open(f'C:\Users\User\Desktop\{f1_name} diff.txt', 'w')
difference = reference_set.difference(compare_set)
for i in difference:
f3.write(i + '\n')
f1.close()
f3.close()
这个想法来自于拖放到cmd
,将用双引号括起来的文件路径复制到其中。我在路径之间使用重复的双引号来创建一个列表,您可以在代码中看到其余部分。
但是,有一个缺点,那就是您不能将多个文件拖在一起,您应该一个一个地拖,但总比没有好。 ;)
我写了一个 python 脚本,它将两个文件作为输入,然后将它们之间的差异作为输出保存在另一个文件中。
我将它绑定到一个批处理文件 .cmd
(见下文)并将批处理文件添加到文本文件的上下文菜单中,所以当我右键单击一个文本文件并 select 它, cmd
window 弹出,我输入要比较的文件地址。
批处理文件内容:
@echo off
cls
python "C:\Users\User\Desktop\Difference of Two Files.py" %1
Python代码:
import sys
import os
f1 = open(sys.argv[1], 'r')
f1_name = str(os.path.basename(f1.name)).rsplit('.')[0]
f2_path = input('Enter the path of file to compare: ')
f2 = open(f2_path, 'r')
f2_name = str(os.path.basename(f2.name)).rsplit('.')[0]
f3 = open(f'{f1_name} - {f2_name} diff.txt', 'w')
file1 = set(f1.read().splitlines())
file2 = set(f2.read().splitlines())
difference = file1.difference(file2)
for i in difference:
f3.write(i + '\n')
f1.close()
f2.close()
f3.close()
现在,我的问题是如何用接受多个文件的拖放解决方案替换第二个文件路径的输入。
我对 python 代码没有任何问题,可以自己扩展它以包含更多文件。我只是不知道如何编辑批处理文件,所以不是通过键入路径只获取一个文件,而是通过拖放来获取多个文件。
非常感谢你的帮助。
终于,我自己想通了! 我 Post 最后的代码,也许它对某人有帮助。
# This script prints those lines in the 1st file that are not in the other added files
# and saves the results into a 3rd file on Desktop.
import sys
import os
f1 = open(sys.argv[1], 'r')
f1_name = str(os.path.basename(f1.name)).rsplit('.')[0]
reference_set = set(f1.read().splitlines())
compare_files = input('Drag and drop files into this window to compare: ')
compare_files = compare_files.strip('"').rstrip('"')
compare_files_list = compare_files.split('\"\"')
compare_set = set()
for file in compare_files_list:
with open(os.path.abspath(file), 'r') as f2:
file_content = set(f2.read().splitlines())
compare_set.update(file_content)
f3 = open(f'C:\Users\User\Desktop\{f1_name} diff.txt', 'w')
difference = reference_set.difference(compare_set)
for i in difference:
f3.write(i + '\n')
f1.close()
f3.close()
这个想法来自于拖放到cmd
,将用双引号括起来的文件路径复制到其中。我在路径之间使用重复的双引号来创建一个列表,您可以在代码中看到其余部分。
但是,有一个缺点,那就是您不能将多个文件拖在一起,您应该一个一个地拖,但总比没有好。 ;)