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
  • 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. – Rostyslav Dzinko Aug 09 '12 at 10:21
  • 1
    `my code is not working` : why? What does(n't) it do what it's supposed to do? – catchmeifyoutry Aug 09 '12 at 10:22
  • do you really want to close the file in each iteration? – tzelleke Aug 09 '12 at 10:25
  • no do not need to close file in each iteration but then how can i exit after writing lines in file? – Ameet Kumar Aug 09 '12 at 10:41
  • I have done the changes but it still I am not able to find solution – Ameet Kumar Aug 09 '12 at 13:50
  • @AmeetKumar: Can you state more clearly what is happening when you run your code? You're giving the desired output (which is important!), but despite trying your code, I can't tell what's going wrong for you. Are you getting an exception? Wrong output? No output? – Blckknght Aug 09 '12 at 14:26
  • @Blckknght: When I run from command prompt I encounters two things 1. C:\Users\Abcd\Desktop>python test.py 5 I get : SyntaxError: invalid syntax 2. When I simply try to run file with out passing argument C:\Users\Abcd\Desktop>python test.py I get : Traceback (most recent call last): File "", line 1, in test.py NameError: name 'test' is not defined – Ameet Kumar Aug 09 '12 at 14:46

1 Answers1

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()
Community
  • 1
  • 1
catchmeifyoutry
  • 7,179
  • 1
  • 29
  • 26