使用另一个python文件在python文件中编辑/添加一行

使用另一个python文件在python文件中编辑/添加一行,python,Python,我有一个python文件,文件中的行是按特定顺序排列的,我试图在文件中的特定位置添加/删除行,然后将其另存为新行 例如: parameter1 = "some code..." parameter2 = "some code..." 我想在这些行之间添加一行(例如参数3)和/或删除其中一行。将文件中的行读取到列表中。然后您可以在该列表中插入或删除,例如 以open('file.py','r')作为文件的: file\u content=file.readlines() 文件\u content

我有一个python文件,文件中的行是按特定顺序排列的,我试图在文件中的特定位置添加/删除行,然后将其另存为新行

例如:

parameter1 = "some code..."
parameter2 = "some code..."

我想在这些行之间添加一行(例如参数3)和/或删除其中一行。

将文件中的行读取到列表中。然后您可以在该列表中插入或删除,例如

以open('file.py','r')作为文件的
:
file\u content=file.readlines()
文件\u content.insert(1,'parameter2=“somecode…”\n')
打开('file.py','w')作为文件:
file.write(“”.join(文件内容))

将文件中的行读入列表。然后您可以在该列表中插入或删除,例如

以open('file.py','r')作为文件的
:
file\u content=file.readlines()
文件\u content.insert(1,'parameter2=“somecode…”\n')
打开('file.py','w')作为文件:
file.write(“”.join(文件内容))

这是否回答了您的问题?这回答了你的问题吗?