我有以下脚本:
import random
def mf():
filename = raw_input("file: ")
string="a"
while (string):
string = raw_input("ID\n")
string += " | "
string += raw_input("Title\n")
string += " | "
string += raw_input("Artist\n")
string += " | "
string += raw_input("Kind\n")
string += " | "
string += raw_input("Year\n")
string += " | "
string += raw_input("Ranking\n")
string += " | "
string += raw_input("Purchased\n")
string += " | "
string += raw_input("c\n")
f = open(filename,'w')
print string
f.write(string)
f.write("garbage")
f.write("\n")
f.close()
string = raw_input("...")
n = random.randint(1,4)
f = open(filename,'w')
for i in range(n):
f.write("\n")
f.close()
它可以很好地写入换行符,当我打印字符串时,我得到了我所期望的,但是“垃圾”和字符串都没有被写入。