我想在字符串中写入一个文本文件,但只有当该字符串已经不在文本文件中。无法写入文件
b = raw_input("IP Adress: ")
os.system('cls')
if(b == '0'):
a = 0
c = raw_input("Player Name: ")
if(c == '0'):
a = 0
if(a != 0):
line = "Text\IPs\{}.txt".format(b)
output_filee = open(line, 'w+')
output_file = open(line, 'r')
lines = output_file.readlines()
for line in lines:
if(line != c):
found = 1
if(found == 1):
output_filee.write(c)
output_file.close()
output_filee.close()
print '"{}" has been added to the IP Address {}'.format(c,b)
上面的代码使新文件在文件夹中,但没有任何内容。 有什么建议吗?
的“发现”变量已经前手定义。 –
循环中的逻辑错误。如果有任何与“c”不同的行,即使“c”与其他行相匹配,它也会设置“found = 1”。 – Barmar
代码中有很多逻辑错误。 –