尝试追加 try 语句时失败
Failure while trying to append in a try statement
所以我有一个小程序可以读取文件并在文件不存在时创建它。
但是当您尝试读取第二个和第三个文件的内容并将其附加到第一个文件时,它会失败。
我在代码中准确标记了它失败的地方。
它总是跳到except部分,我没有把它包括在这里因为它似乎没有必要(except部分)
with open ('lista1.txt','r') as file_1:
reader_0 = file_1.readlines() #reads a list of searchterms, the first search term of this list is "gt-710"
for search in reader_0:
# creates the txt string component of the file to be created, this is the first one
file_0 = search.replace("\n","") +".txt"
file_1 = str(file_0.strip())
# creates the txt string component of the file to be created, this is the second one
files_2 = search.replace("\n","") +"2.txt"
file_2 = str(files_2.strip())
# creates the txt string component of the file to be created, this is the second one
files_3 = search.replace("\n","") +"3.txt"
file_3 = str(files_3.strip())
try: #if the file named the same as the searchterm exists, read its contents
file = open(file_1,"r")
file2 = open(file_2,"r")
file3 = open(file_3,"r")
file_contents = file.readlines()
file_contents2 = file2.readlines()
file_contents3 = file3.readlines()
file = open(file_1,"a") #appends the contents of file 3 and file 2 to file 1
print("im about here")
file.write(file_contents2) #fails exactly here I don't know why
file.write(file_contents3)
file2 = open(file_2,"w+")
file2.write(file_contents)
file3 = open(file_3,"w+")
file3.write(file_contents2)
您使用 file = open(file_1, 'r')
从 file_
开始读取,然后以追加模式再次打开它,而没有关闭第一个 I/O 操作 - 当您尝试写入时导致失败在以读取模式打开时查找。
更改文件 reading/writing 以使用 less error-prone with open
语法,如下所示:
with open(file_1, 'r') as file_handle:
file_contents = file_handle.read()
with open(file_2, 'r') as file_handle:
file_contents2 = file_handle.read()
with open(file_3, 'r') as file_handle:
file_contents3 = file_handle.read()
with open(file_1, 'a') as file_handle:
file_handle.write(file_contents2)
# etc.
此语法使文件何时不再打开以及打开状态非常明显。
它在您提到的那一点失败的原因是您试图将列表写入文件(而不是字符串)。 file2.readlines()
returns 一个字符串列表,每个字符串都是自己的行,因此要修复此问题,请将所有 readlines
更改为 filexxx.read()
,其中 returns 整个文件内容作为字符串.
我还建议对其他答案状态进行更改以使您的代码更 readable/robust。
所以我有一个小程序可以读取文件并在文件不存在时创建它。 但是当您尝试读取第二个和第三个文件的内容并将其附加到第一个文件时,它会失败。 我在代码中准确标记了它失败的地方。
它总是跳到except部分,我没有把它包括在这里因为它似乎没有必要(except部分)
with open ('lista1.txt','r') as file_1:
reader_0 = file_1.readlines() #reads a list of searchterms, the first search term of this list is "gt-710"
for search in reader_0:
# creates the txt string component of the file to be created, this is the first one
file_0 = search.replace("\n","") +".txt"
file_1 = str(file_0.strip())
# creates the txt string component of the file to be created, this is the second one
files_2 = search.replace("\n","") +"2.txt"
file_2 = str(files_2.strip())
# creates the txt string component of the file to be created, this is the second one
files_3 = search.replace("\n","") +"3.txt"
file_3 = str(files_3.strip())
try: #if the file named the same as the searchterm exists, read its contents
file = open(file_1,"r")
file2 = open(file_2,"r")
file3 = open(file_3,"r")
file_contents = file.readlines()
file_contents2 = file2.readlines()
file_contents3 = file3.readlines()
file = open(file_1,"a") #appends the contents of file 3 and file 2 to file 1
print("im about here")
file.write(file_contents2) #fails exactly here I don't know why
file.write(file_contents3)
file2 = open(file_2,"w+")
file2.write(file_contents)
file3 = open(file_3,"w+")
file3.write(file_contents2)
您使用 file = open(file_1, 'r')
从 file_
开始读取,然后以追加模式再次打开它,而没有关闭第一个 I/O 操作 - 当您尝试写入时导致失败在以读取模式打开时查找。
更改文件 reading/writing 以使用 less error-prone with open
语法,如下所示:
with open(file_1, 'r') as file_handle:
file_contents = file_handle.read()
with open(file_2, 'r') as file_handle:
file_contents2 = file_handle.read()
with open(file_3, 'r') as file_handle:
file_contents3 = file_handle.read()
with open(file_1, 'a') as file_handle:
file_handle.write(file_contents2)
# etc.
此语法使文件何时不再打开以及打开状态非常明显。
它在您提到的那一点失败的原因是您试图将列表写入文件(而不是字符串)。 file2.readlines()
returns 一个字符串列表,每个字符串都是自己的行,因此要修复此问题,请将所有 readlines
更改为 filexxx.read()
,其中 returns 整个文件内容作为字符串.
我还建议对其他答案状态进行更改以使您的代码更 readable/robust。