open ('the_file_name.txt', 'a')
def example ():
print ("hello, world!", file = 'the_file_name.txt')
times = int (input ("how many 'hello worlds'?"))
for c in range (times):
example ()
def example():
return "hello, world!"
times = int(input("how many 'hello worlds'? "))
data = []
for c in range(times):
data.append(example())
file = open('finename.txt', 'w')
with file as f:
for line in data:
f.write(f'{line}\n')
如果要在每个循环上写入一个新文件,请以“w”的形式打开该文件,以便
另外,如果您想在不清除文件的情况下写入,请将其作为“a”打开,即append,因此
但请确保该文件已存在
你当然可以! 首先,您需要打开该文件,为此编写的程序必须位于该文件的文件夹中。以下是如何做到这一点:
open ('the_file_name.txt', 'a')
(“a”作为追加)。当然,你还没有写信。您可以通过将文件argentum添加到print命令来实现这一点:print ('hello, world!', File = 'the_file_name.txt')
完整代码:您可以重新编写一点代码,但现在应该将“hello,world”文本保存在新行上
相关问题 更多 >
编程相关推荐