[英]How to run a python script from a text file context menu and compare it to other text files?
I wrote a python script that takes two files as input and then saves the difference between them as output in another file. 我写了一个python脚本,将两个文件作为输入,然后将它们之间的差异作为输出保存在另一个文件中。
I bound it to a batch file .cmd
(see below) and added the batch file to context menu of text files, so when I right-click on a text file and select it, a cmd
window pops up and I type the address of the file to compare. 我将其绑定到批处理文件.cmd
(请参见下文),并将该批处理文件添加到文本文件的上下文菜单中,因此,当我右键单击文本文件并将其选中时,将弹出一个cmd
窗口,并键入要比较的文件。
Batch file content: 批处理文件内容:
@echo off
cls
python "C:\Users\User\Desktop\Difference of Two Files.py" %1
Python Code: 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()
Now, my question is how can I replace the typing of 2nd file path with a drag and drop solution that accepts more than one file. 现在,我的问题是如何用接受多个文件的拖放解决方案替换第二个文件路径的键入。
I don't have any problem with python code and can extend it myself to include more files. 我对python代码没有任何问题,可以自己扩展它以包含更多文件。 I just don't know how to edit the batch file so instead of taking just one file by typing the path, it takes several files by drag and drop. 我只是不知道如何编辑批处理文件,因此它不是通过输入路径仅提取一个文件,而是通过拖放来提取多个文件。
I would appreciate your help. 多谢您的协助。
Finally, I've figured it out myself! 最后,我自己弄清楚了! I Post the final code, maybe it helps somebody. 我发布了最终代码,也许对某人有帮助。
# 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()
The idea came from this fact that drag and drop to cmd
, copies the file path surrounded with double-quotes into it. 产生这个想法的原因是,将其拖放到cmd
,然后将用双引号引起来的文件路径复制到其中。 I used the repeated double-quotes between paths to create a list, and you can see the rest in the code. 我使用了路径之间重复的双引号来创建列表,您可以在代码中看到其余的内容。 However, there's a downside and it's that you can't drag multiple files together and you should do that one by one, but it's better than nothing. 但是,它有一个缺点,那就是您不能将多个文件拖在一起,应该一个一个地做,但这总比没有好。 ;) ;)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.