在linux上向配置文件追加数据

2024-06-11 07:41:12 发布

您现在位置:Python中文网/ 问答频道 /正文

我想将数据附加到我的file.config。我还使用linux,它将请求对文件的任何配置的权限。在终端中,我可以写入sudo nano file.config并进行更改。

期望值:my file.cofig看起来像这样

#
#info ...
#
#
[Section]
#info
I want to append data right here to the end of the file

我尝试使用configparser模块:

configparser = configparser.ConfigParser()   
configparser['Section'] = {'data':'123'}
configFilePath = '/etc/file.conf'

with open(configFilePath, 'a') as file_conf:
  configparser.write(file_conf)

这会将信息添加为字典,而不是常规文件.txt

#
#info ...
#
#
[Section]
#info
[Section]
data = 123

按照评论中的要求:需要sudo python3 file.py(visual studio未处于su模式)


Tags: 文件theto数据infoconfig权限data