如果这是一个愚蠢的问题,我很抱歉,但我没有太多的Python经验
我有比较文件的功能
def compare_files(file1, file2):
fname1 = file1
fname2 = file2
# Open file for reading in text mode (default mode)
f1 = open(fname1)
f2 = open(fname2)
# Print confirmation
#print("-----------------------------------")
#print("Comparing files ", " > " + fname1, " < " +fname2, sep='\n')
#print("-----------------------------------")
# Read the first line from the files
f1_line = f1.readline()
f2_line = f2.readline()
# Initialize counter for line number
line_no = 1
# Loop if either file1 or file2 has not reached EOF
while f1_line != '' or f2_line != '':
# Strip the leading whitespaces
f1_line = f1_line.rstrip()
f2_line = f2_line.rstrip()
# Compare the lines from both file
if f1_line != f2_line:
########## If a line does not exist on file2 then mark the output with + sign
if f2_line == '' and f1_line != '':
print ("Line added:Line-%d" % line_no + "-"+ f1_line)
#otherwise output the line on file1 and mark it with > sign
elif f1_line != '':
print ("Line changed:Line-%d" % line_no + "-"+ f1_line)
########### If a line does not exist on file1 then mark the output with + sign
if f1_line == '' and f2_line != '':
print ("Line removed:Line-%d" % line_no + "-"+ f1_line)
# otherwise output the line on file2 and mark it with < sign
#elif f2_line != '':
#print("<", "Line-%d" % line_no, f2_line)
# Print a blank line
#print()
#Read the next line from the file
f1_line = f1.readline()
f2_line = f2.readline()
#Increment line counter
line_no += 1
# Close the files
f1.close()
f2.close()
我想将函数输出打印到文本文件
result=compare_files("1.txt", "2.txt")
print (result)
Line changed:Line-1-aaaaa
Line added:Line-2-sss
None
我试过以下:
f = open('changes.txt', 'w')
f.write(str(result))
f.close
但只有None打印到changes.txt
我正在使用“workaround”sys.stdout,但是想知道是否有其他方法而不是重定向打印输出。
如果在函数输出中我指定return而不是print,那么我只得到第一个输出行(Line changed:Line-1-aaaaa)到changes.txt
因为您在默认情况下没有返回任何内容,所以函数会返回None
,以便反映在changes.txt
文件中。您可以创建一个存储所需输出的变量并将其返回。
def compare_files(file1, file2):
fname1 = file1
fname2 = file2
# Open file for reading in text mode (default mode)
f1 = open(fname1)
f2 = open(fname2)
output_string = ""
# Print confirmation
# print("-----------------------------------")
# print("Comparing files ", " > " + fname1, " < " +fname2, sep='\n')
# print("-----------------------------------")
# Read the first line from the files
f1_line = f1.readline()
f2_line = f2.readline()
# Initialize counter for line number
line_no = 1
# Loop if either file1 or file2 has not reached EOF
while f1_line != '' or f2_line != '':
# Strip the leading whitespaces
f1_line = f1_line.rstrip()
f2_line = f2_line.rstrip()
# Compare the lines from both file
if f1_line != f2_line:
########## If a line does not exist on file2 then mark the output with + sign
if f2_line == '' and f1_line != '':
print("Line added:Line-%d" % line_no + "-" + f1_line)
output_string += "Line added:Line-%d" % line_no + "-" + f1_line + "\n"
# otherwise output the line on file1 and mark it with > sign
elif f1_line != '':
print("Line changed:Line-%d" % line_no + "-" + f1_line)
output_string += "Line changed:Line-%d" % line_no + "-" + f1_line +"\n"
########### If a line does not exist on file1 then mark the output with + sign
if f1_line == '' and f2_line != '':
print("Line removed:Line-%d" % line_no + "-" + f1_line)
output_string += "Line removed:Line-%d" % line_no + "-" + f1_line +"\n"
# otherwise output the line on file2 and mark it with < sign
# elif f2_line != '':
# print("<", "Line-%d" % line_no, f2_line)
# Print a blank line
# print()
# Read the next line from the file
f1_line = f1.readline()
f2_line = f2.readline()
# Increment line counter
line_no += 1
# Close the files
f1.close()
f2.close()
return output_string
你的'compare_files'函数不返回任何内容,因此没有任何内容写入文件。使函数“返回”某些东西,它应该工作。
你的功能没有返回任何东西,所以你打印'无'。如果你想让所有的print都转到一个文件而不是stdout,就像默认情况下那样,你可以将每个print语句转换为返回值。
或者你可以像在here中那样使用整个程序的重定向。
你的compare_files()
只是打印,但没有传递任何东西给它的来电者。
如果要将一个项目传递给调用者,请使用return
。你的功能流程就此结束。
如果你想将几个项目传递给调用者,你可以使用yield
。使用yield
将您的功能转换为生成器功能。调用生成器函数会生成一个可以迭代的生成器对象。
例:
def produce_strings():
for i in ['a', 'b', 'c']:
yield i + "x"
result = "\n".join(produce_strings())
print(result) # prints a line end separated string made of "ax", "bx" and "cx".