0
我想通過在Python中通過命令行參數傳遞整數編輯文本文件。然而,我的代碼不工作,有人能指出我錯在哪裏。通過Python中的命令行參數編輯文本文件
import sys, argparse
def main(argv=None):
if argv is None:
argv=sys.argv[1:]
p = argparse.ArgumentParser(description="Editing omnetpp.ini")
p.add_argument('arg1', action='store', default= 1, type=int, help="number of clients")
args = p.parse_args(argv)
n = args.arg1
f = open('C:\\Users\Abcd\Desktop\Omnet\omnetpp.ini', 'a')
for i in range(n):
f.write('*.voipClient['+str(i)+'].udpApp['+str(i)+'].destAddresses = "voipGateway"\n')
f.write('*.voipGateway.udpApp['+str(i)+'].destAddresses = "voipClient['+str(i)+']"\n')
f.close()
如果整數5通過命令行參數傳遞就應該添加下面的文本文件,行未發生 輸出
*.voipClient[0].udpApp[0].destAddresses = "voipGateway"
*.voipGateway.udpApp[0].destAddresses = "voipClient[0]"
*.voipClient[1].udpApp[1].destAddresses = "voipGateway"
*.voipGateway.udpApp[1].destAddresses = "voipClient[1]"
*.voipClient[2].udpApp[2].destAddresses = "voipGateway"
*.voipGateway.udpApp[2].destAddresses = "voipClient[2]"
*.voipClient[3].udpApp[3].destAddresses = "voipGateway"
*.voipGateway.udpApp[3].destAddresses = "voipClient[3]"
*.voipClient[4].udpApp[4].destAddresses = "voipGateway"
*.voipGateway.udpApp[4].destAddresses = "voipClient[4]"
我以下步驟:
- 代碼保存在test.py
- 從命令行C:\ Users \ Abcd \ Desktop> python test.py 5
如果這是所有模塊的代碼,我可以指出,您實際上並沒有運行main函數。它已被定義但未被調用。在函數定義之後,在模塊級添加函數調用:main()。 – 2012-08-09 10:21:34
'我的代碼不工作':爲什麼? (它)做它應該做的事情是什麼? – catchmeifyoutry 2012-08-09 10:22:41
你真的想在每次迭代中關閉文件嗎? – 2012-08-09 10:25:09