0

I want to edit text file by passing integer number via command line argument in Python. However my code is not working, can some one point me where I am wrong.

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

If integer number 5 is passed via command line argument then it should add following lines in text file, which is not happening Output

*.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]"

I am following these steps:

  1. Code is saved in test.py
  2. From command line C:\Users\Abcd\Desktop>python test.py 5
7
  • 1
    If that's all module's code, I can point out that you don't actually run function main. It's defined but is not called. Add function call: main() at module level after function definition. Commented Aug 9, 2012 at 10:21
  • 1
    my code is not working : why? What does(n't) it do what it's supposed to do? Commented Aug 9, 2012 at 10:22
  • do you really want to close the file in each iteration? Commented Aug 9, 2012 at 10:25
  • no do not need to close file in each iteration but then how can i exit after writing lines in file? Commented Aug 9, 2012 at 10:41
  • I have done the changes but it still I am not able to find solution Commented Aug 9, 2012 at 13:50

1 Answer 1

1

Don't close the file in the loop, as soon as it is closed you cannot write to it anymore (in fact, an error should be thrown if you try to write to a closed file object). Instead, close it after the loop. Also, to put each sentence on a new line, end the string with the newline symbol \n (sort of pressing "ENTER").

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

EDIT

By the way, as Rostyslav Dzinko said in the comments, the way you defined your code is not how you define a main function. In fact, try something like this (see also this SO question):

if __name__ == '__main__':
    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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.