如何将命令行参数打印到文件

时间:2016-07-04 07:26:50

标签: python command-line

我从命令行接受4个参数,并希望用它来在python中创建一个文件。

我的语法是:

def configuredhcp(self,**kwargs):
                ''' Get the input parameters '''

                 client_ip = kwargs.get('client_ip',None)
                 client_mask = kwargs.get('client_mask',None)
                 option_routers = kwargs.get('option_routers',None)
                 option_broadcast = kwargs.get('option_broadcast',None)
                 range_ip = kwargs.get('range_ip',None)

                 infile = open('dhcpconf.txt', 'r+')
                 infile.write("Subnet  %s netmask %s " % (client_ip ,client_mask)
                 infile.write("option routers %s " % (option_routers)
                 infile.write("option broadcast-address %s; " % (option_broadcast)
                 infile.write(" range %s;" % (range_ip)
                 infile.close()

我在行

中遇到语法错误
  

infile.write("选项路由器%s"%(option_routers)

1 个答案:

答案 0 :(得分:0)

你在每次写作中都错过了')'个大括号。试试这个: -

def configuredhcp(self, **kwargs):
    ''' Get the input parameters '''

    client_ip = kwargs.get('client_ip', None)
    client_mask = kwargs.get('client_mask', None)
    option_routers = kwargs.get('option_routers', None)
    option_broadcast = kwargs.get('option_broadcast', None)
    range_ip = kwargs.get('range_ip', None)

    infile = open('dhcpconf.txt', 'r+')
    infile.write("Subnet  %s netmask %s " % (client_ip, client_mask))
    infile.write("option routers %s " % (option_routers))
    infile.write("option broadcast-address %s; " % (option_broadcast))
    infile.write(" range %s;" % (range_ip))
    infile.close()