2016-07-04 82 views
0

我接受來自命令行的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( 「選項路由器%的」 %(option_routers)

+0

如果這是確切的代碼,則沒有匹配的右括號。 – Lafexlos

回答

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() 
+0

非常感謝,工作 –

+0

乾杯:)....... – Deca

相關問題