Python,将文件写入两次
我正在尝试获取我的代码,这样它将更新Y7,Set1.txt,但它会继续在其中写入数据,+我添加的新数据。 我的代码如下:Python,将文件写入两次,python,python-3.x,Python,Python 3.x,我正在尝试获取我的代码,这样它将更新Y7,Set1.txt,但它会继续在其中写入数据,+我添加的新数据。 我的代码如下: print("Registration System") print("Make sure their is atleast one name in register!") password = ("admin") check = input("The Administration Password is: ") if check == password: input
print("Registration System")
print("Make sure their is atleast one name in register!")
password = ("admin")
check = input("The Administration Password is: ")
if check == password:
input("Enter any key to continue ")
print("If their is no names in the register.txt\nThe script will stop working here!")
set1 = list()
set2 = list()
set3 = list()
set4 = list()
set5 = list()
with open("register.txt", "r") as register:
registers = [line.rstrip("\n") for line in register]
for pop in registers:
print(pop)
choice = input(
'Where would you like it to go? (Enter 1 - set1, enter 2 - set2)')
if choice == '1':
set1.append(pop)
elif choice == '2':
set2.append(pop)
elif choice == '3':
set3.append(pop)
elif choice == '4':
set4.append(pop)
elif choice == '5':
set5.append(pop)
with open("Y7,Set2.txt", "r") as Y7R:
Y7 = [line.rstrip("\n") for line in Y7R]
with open("Y7,Set2.txt", "w") as Y7Y:
data = (str(Y7) + str(set2))
Y7Y.writelines(data)
with open("Y7,Set1.txt", "r") as d:
Y7S = [line.rstrip("\n") for line in d]
with open("Y7,Set1.txt", "w") as Y7D:
data2 = str(Y7S) + str(set1)
Y7D.writelines(data2)
else:
print("Access Denied!")
但是,文本文件会继续打印以前的值,+很多///////
无论如何,我可以让我的代码只向该文件添加信息,而不添加所有///和以前的名称?谢谢
文件中发生的情况是:
['[\'[\\\'[\\\\\\\'[\\\\\\\\\\\\\\\'[\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\\'[\\\\\\\
PaulBrennan\\\\\\\\\
使用“a”
模式打开文件<代码>“r”模式用于仅需读取文件,而“w”
仅需写入文件(具有相同名称的现有文件将被擦除)
你可以查一下电话号码
open("Y7,Set2.txt", "a")