Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/15.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cassandra/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在附加文件时,如何使用Python在文本文件中的旧信息下添加新信息_Python_Python 3.x - Fatal编程技术网

在附加文件时,如何使用Python在文本文件中的旧信息下添加新信息

在附加文件时,如何使用Python在文本文件中的旧信息下添加新信息,python,python-3.x,Python,Python 3.x,我试图创建或附加一个文本文件,如果它已经存在,我可以在研究后这样做。然而,我一直在试图找出如何在旧信息下添加信息 例如,我有一个文本文件,其中包含sam10 name = input('Your name?') squad = group number = input('A number?') target = open(squad, 'a') target.write(name) target.write(str(number)) 如果我运行这一切,它会添加到文本文件中,并最终生成一个文本文

我试图创建或附加一个文本文件,如果它已经存在,我可以在研究后这样做。然而,我一直在试图找出如何在旧信息下添加信息

例如,我有一个文本文件,其中包含
sam10

name = input('Your name?')
squad = group
number = input('A number?')
target = open(squad, 'a')
target.write(name)
target.write(str(number))
如果我运行这一切,它会添加到文本文件中,并最终生成一个文本文件(称为组),如下所示:
sam10james5

我如何编辑这段代码,从而在
sam10
下面找到
james5
? 因此,当我打开文本文件时,我看到它是这样的:

sam10
james5

您只需添加一个
“\n”

更改:

target.write(name)
与:


您只需添加一个
“\n”

更改:

target.write(name)
与:


您只是缺少添加“新行”:

target.write(名称)
target.write(str(number)+'\n')


write
只需将提供的确切字符添加到文件中,仅此而已。您需要根据需要添加新行。

您只是缺少添加“新行”:

target.write(名称)
target.write(str(number)+'\n')


write
只需将提供的确切字符添加到文件中,仅此而已。您需要根据需要添加新行。

谢谢,这么简单的回答让我感到尴尬。谢谢,这么简单的回答让我感到尴尬。