无法循环文件以在 python 中执行差异
Unable to loop the files to perform diff in python
我是 python 的新手。我正在编写一个 python 脚本来查找 2 html file1: beta.vidup.me-log-2016-09-21-17:43:28.html 和 file2: beta.vidup.me-log-2016-09-21-17:47:48.html.
关于我的文件组织的想法:我有 2 个目录 2016-09-21 和 2016-09-22。 file1: beta.vidup.me-log-2016-09-21-17:43:28.html 存在于 dir1 和 file2: beta.vidup.me-log-2016-09-21-17:47: 48.html 存在于 dir2 中。
下面是我的片段:
dir1 = raw_input("Enter date of Archive folder to compare with in format yyyy-mm-dd---->\n")
dir2 = raw_input("Enter date of folder to compare in format yyyy-mm-dd----->\n")
now = datetime.now()
folder_output = '/home/diff_output/{}'.format(now.strftime('%Y-%m-%d'))
mkdir(folder_output)
fname1 = '/home/output/%s/beta.vidup.me-log-2016-09-21-17:43:28.html'%dir1
fname2 = '/home/output/%s/beta.vidup.me-log-2016-09-21-17:47:48.html'%dir2
# Open file for reading in text mode (default mode)
f1 = open(fname1)
f2 = open(fname2)
cmd = "diff "+fname1+'\t'+fname2
curl = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
file_data = curl.stdout.read()
print file_data
fname1.close()
fname2.close()
我希望只使用 subprocess 模块来执行 diff。
我希望我的代码从 dir1 中获取 fname1,从 dir2 中获取 fname2 并执行差异并将其输出到一个文件夹,然后循环返回以选择 dir1 中的下一个文件用于 fname1 并从 dir2 中选择下一个文件用于 fname2 并执行再次差异。
提前感谢您的宝贵时间和建议。
以下是您需要的功能和示例。使用 for
循环的逻辑合并它们。
您可以使用 subprocess.check_output()
从命令中获取输出。尝试:
cmd = ["diff", fname1, +fname2]
output = subprocess.check_output(cmd)
print output
如果要将其写入文件:
with open('/paht/to/file', 'w+') as f:
f.write(output)
为了获取目录中的文件列表,请使用 os
模块的 listdir()
、isfile
和 join
函数。例如:
from os import listdir
from os.path import isfile, join
only_files = [f for f in listdir('path') if isfile(join('path', f))]
# only_files will contain list of all files in 'path' path
正如您所说,您对循环不太了解。为您提供有关循环应如何工作的基本概念。下面是示例(不要复制,试着理解每一行。这对你以后会有帮助):
for f1, f2 in zip(file_list_1, file_list_2): # takes first, second, etc files corresponding to each list
output = subprocess.check_output(['diff', f1, f2]) # generate diff of both file
with open('diff-{}-{}'.format(f1, f2), 'w+') as f:
f.write(output) # write the diff to third file
根据您的要求修改上述逻辑。
我是 python 的新手。我正在编写一个 python 脚本来查找 2 html file1: beta.vidup.me-log-2016-09-21-17:43:28.html 和 file2: beta.vidup.me-log-2016-09-21-17:47:48.html.
关于我的文件组织的想法:我有 2 个目录 2016-09-21 和 2016-09-22。 file1: beta.vidup.me-log-2016-09-21-17:43:28.html 存在于 dir1 和 file2: beta.vidup.me-log-2016-09-21-17:47: 48.html 存在于 dir2 中。
下面是我的片段:
dir1 = raw_input("Enter date of Archive folder to compare with in format yyyy-mm-dd---->\n")
dir2 = raw_input("Enter date of folder to compare in format yyyy-mm-dd----->\n")
now = datetime.now()
folder_output = '/home/diff_output/{}'.format(now.strftime('%Y-%m-%d'))
mkdir(folder_output)
fname1 = '/home/output/%s/beta.vidup.me-log-2016-09-21-17:43:28.html'%dir1
fname2 = '/home/output/%s/beta.vidup.me-log-2016-09-21-17:47:48.html'%dir2
# Open file for reading in text mode (default mode)
f1 = open(fname1)
f2 = open(fname2)
cmd = "diff "+fname1+'\t'+fname2
curl = subprocess.Popen(cmd,shell=True,stdout=subprocess.PIPE)
file_data = curl.stdout.read()
print file_data
fname1.close()
fname2.close()
我希望只使用 subprocess 模块来执行 diff。
我希望我的代码从 dir1 中获取 fname1,从 dir2 中获取 fname2 并执行差异并将其输出到一个文件夹,然后循环返回以选择 dir1 中的下一个文件用于 fname1 并从 dir2 中选择下一个文件用于 fname2 并执行再次差异。
提前感谢您的宝贵时间和建议。
以下是您需要的功能和示例。使用 for
循环的逻辑合并它们。
您可以使用 subprocess.check_output()
从命令中获取输出。尝试:
cmd = ["diff", fname1, +fname2]
output = subprocess.check_output(cmd)
print output
如果要将其写入文件:
with open('/paht/to/file', 'w+') as f:
f.write(output)
为了获取目录中的文件列表,请使用 os
模块的 listdir()
、isfile
和 join
函数。例如:
from os import listdir
from os.path import isfile, join
only_files = [f for f in listdir('path') if isfile(join('path', f))]
# only_files will contain list of all files in 'path' path
正如您所说,您对循环不太了解。为您提供有关循环应如何工作的基本概念。下面是示例(不要复制,试着理解每一行。这对你以后会有帮助):
for f1, f2 in zip(file_list_1, file_list_2): # takes first, second, etc files corresponding to each list
output = subprocess.check_output(['diff', f1, f2]) # generate diff of both file
with open('diff-{}-{}'.format(f1, f2), 'w+') as f:
f.write(output) # write the diff to third file
根据您的要求修改上述逻辑。