簡體   English   中英

在python中創建一個新的INI文件

[英]Create a new INI-file in python

我需要在我的代碼中創建一個函數,它執行以下操作:

  1. 創建一個新的 ini 文件到我的代碼所在的相同位置路徑(例如:C:\\Users<user name>\\source\\repos
  2. 我想讓它詢問 [用戶名]、[組織]、[服務器]、[端口] 和 [文件名] -> 將其保存/覆蓋到路徑

所以我希望它像這樣開始,但我不知道如何將其寫入新文件

### create a function that creates new file according to user input and save it to directory

def Osio8():   
    print ("Create new INI file \n")   
    
    ## asks for user input
    input_name = str(input("Give user name:" ))
    input_organization = str(input("Organisation name: "))
    input_server = str(input("Server IP address: "))
    input_port = str(input("Port number: "))
    input_file = str (input("File name:" ))
    fo = open (input_file, "w")


    ## writes the information to the file
    fo.write = multi_line_string = ("; last modified 1 April 2001 by John    Doe\n"
    "[owner]\n"
    "name=" + input_name + "\n"
    "organization=" + input_organization + "\n"
    "\n"
    "[database]\n"
    "; use IP address in case network name resolution is not working\n"
    "server=" + input_server + "\n"
    "port=" + input_port + "\n"
    "file='" + input_file + "'\n")

目前,這會創建一個具有給定文件名的新文件,但不會存儲任何內容。 我究竟做錯了什么?

首先,詢問用戶所需的文件名,只有在收到答案后才以所需的模式打開文件(可能是“w”)所有這些,然后才使用open()

您可以使用configparser

#!/usr/bin/env python3

import configparser

def Osio8():
    print('Create new INI file')

    input_config_file = input('Config file name: ')

    input_name = input('User name: ')
    input_organization = input('Organisation name: ')
    input_server = input('Server IP address: ')
    input_port = input('Port number: ')
    input_file = input('File name: ')

    config = configparser.ConfigParser(allow_no_value=True)
    config.optionxform = str

    config.add_section('owner')
    config.set('owner', '; last modified 1 April 2001 by John Doe', None)
    config['owner']['name'] = input_name
    config['owner']['organization'] = input_organization

    config.add_section('database')
    config.set('database', '; use IP address in case network name resolution is not working', None)
    config['database']['server'] = input_server
    config['database']['port'] = input_port
    config['database']['file'] = input_file

    with open(input_config_file, 'w') as configfile:
        config.write(configfile)

if __name__ == '__main__':
    Osio8()
$ ./so64756020.py
Create new INI file
Config file name: newconfig.ini
User name: Test user name
Organisation name: Test org name
Server IP address: 1.2.3.4
Port number: 1234
File name: Test file name

$ cat newconfig.ini
[owner]
; last modified 1 April 2001 by John Doe
name = Test user name
organization = Test org name

[database]
; use IP address in case network name resolution is not working
server = 1.2.3.4
port = 1234
file = Test file name 

編輯(基於不被“允許”使用configparser ):

然后,您將不得不“手動”創建文件內容(作為練習很好,但可能除此之外別無其他)。

#!/usr/bin/env python3
  
import configparser

def Osio8():
    print('Create new INI file')

    input_config_file = input('Config file name: ')

    input_name = input('User name: ')
    input_organization = input('Organisation name: ')
    input_server = input('Server IP address: ')
    input_port = input('Port number: ')
    input_file = input('File name: ')

    contents = f"""[owner]
; last modified 1 April 2001 by John Doe
name = {input_name}
organization = {input_organization}

[database]
; use IP address in case network name resolution is not working        
server = {input_server}
port = {input_port}
file = {input_file}
"""

    with open(input_config_file, 'w') as configfile:
        configfile.write(contents + '\n')

if __name__ == '__main__':
    Osio8()

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM