Bonnie_9999 发表于 2020-8-6 13:34:06

python 零基础入门学习29节课后作业求解

f1 = open("F:/something.txt")
f2 = open("F:/poetry.txt")
   
str1 = []
str2 = []
for each_line in f1:
    str1.append(each_line)
for each_line in f2:
    str2.append(each_line)
   
length = len(str1)
f = open("F:/difference.txt","w")
for i in range(length):
    if str1 != str2:
      string = ("第",i,"行不一样")
      f.wtritelines(string)
    f.close()
   

f = open("F:/difference.txt")
f.read()



AttributeError                            Traceback (most recent call last)
<ipython-input-8-e82872c8ac30> in <module>
   14   if str1 != str2:
   15         string = ("第",i,"行不一样")
---> 16         f.wtritelines(string)
   17   f.close
   18

AttributeError: '_io.TextIOWrapper' object has no attribute 'wtritelines'

zltzlt 发表于 2020-8-6 13:34:30

方法名拼错了,应该是 writeline 不是 wtriteline

Bonnie_9999 发表于 2020-8-6 13:37:21

f1 = open("F:/something.txt")
f2 = open("F:/poetry.txt")
   
str1 = []
str2 = []
for each_line in f1:
    str1.append(each_line)
for each_line in f2:
    str2.append(each_line)
   
length = len(str1)
f = open("F:/difference.txt","w")
for i in range(length):
    if str1 != str2:
      string = ("第",i,"行不一样")
      f.writelines(string)
    f.close()
   

f = open("F:/difference.txt")
f.read()


ValueError                              Traceback (most recent call last)
<ipython-input-1-8c247e5c6369> in <module>
   14   if str1 != str2:
   15         string = ("第",i,"行不一样")
---> 16         f.writelines(string)
   17   f.close()
   18

ValueError: I/O operation on closed file.

zltzlt 发表于 2020-8-6 13:38:34

Bonnie_9999 发表于 2020-8-6 13:37
f1 = open("F:/something.txt")
f2 = open("F:/poetry.txt")
   


这样试试:


f1 = open("F:/something.txt")
f2 = open("F:/poetry.txt")
   
str1 = []
str2 = []
for each_line in f1:
    str1.append(each_line)
for each_line in f2:
    str2.append(each_line)
   
length = len(str1)
f = open("F:/difference.txt","w")
for i in range(length):
    if str1 != str2:
      string = ("第",i,"行不一样")
      f.writelines(string)
f.close()
   

f = open("F:/difference.txt")
f.read()

Bonnie_9999 发表于 2020-8-6 13:49:21

谢谢,明白了
页: [1]
查看完整版本: python 零基础入门学习29节课后作业求解