#1
  1. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    19
    Rep Power
    0

    Help to finish my code


    Can someone please finish my code?

    Question 1

    tobi1 and tobi2 are calculations and the values of them are changing everytime.
    In this example they have the same values.
    Only if they have the same values it must print appa to file app1.py and appb to file app2.py
    If they don't have the same value nothing must be done.
    I want to do this 30 times.
    The second time it must print appa to file app3.py and appb to file app4.py
    The third time it must print appa to file app5.py and appb to file app6.py etc....
    After 30 times I must have 60 files and the code must print stop.

    Question 2

    appa and appb are values generated by another file and the values of them are changing everytime.
    If I restart my code several times like this execfile("lot1.py") where appa and appb are opened then
    I want to see that appa and appb are the new generated values.
    But it's not possible to change their values.They stay always the first value given.

    -----------------------------------

    tobi1=4
    tobi2=4

    if tobi1 is tobi2:


    # read a file
    for line in file('appa.py', 'r'):
    med=line.strip()
    appa = float(med)
    print appa

    # read a file
    for line in file('appb.py', 'r'):
    med=line.strip()
    appb = float(med)
    print appb
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    OK, I got this far with your first problem; see, if you can debug it and get the absolute result:
    Code:
    tobi1 = 4
    tobi2 = 4
    appa = 'Anything you want.'
    appb = 'Anything you like.'
    
    if tobi1 == tobi2:
        fileName = 'app1.py'
        fileName2 = 'app2.py'
        count = 0
        fileCount = 3
        fileCount2 = 4
        while count <= 29:
            file = open(fileName, 'w')
            file2 = open(fileName2, 'w')
            file.write(appa)
            file.close()
            file2.write(appb)
            file2.close()
            fileName = list(fileName)
            fileName[3] = str(fileCount)
            fileName = ''.join(fileName)
            fileName2 = list(fileName2)
            fileName2[3] = str(fileCount2)
            fileName2 = ''.join(fileName2)
            fileCount += 2
            fileCount2 += 2
            count += 1
        print('Stop!')
    Last edited by Akshat1; May 27th, 2013 at 01:39 PM.
  4. #3
  5. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    19
    Rep Power
    0
    I got this error

    File "lot1.py", line 420, in <module>
    file.write(appa)
    TypeError: expected a character buffer object

    It also print large numbers like app6208642086420864208642086420.py

    I must restart the code (lot1.py) many times till tobi1=tobi2 that's normal,
    therefore maybe it's not possible to make a counter because when I restart a few times using execfile("lot1.py") the counter is always reset to 0

    And my biggest problem is when I restart ,so open the files 'appa.py' or 'appb.py' again it still shows the same first value.
    I know the values are changed in the files but the code don't accept to interpret and show these new values.
    without these values I can't do anything.
  6. #4
  7. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by Filip.V
    I got this error

    File "lot1.py", line 420, in <module>
    file.write(appa)
    TypeError: expected a character buffer object

    It also print large numbers like app6208642086420864208642086420.py

    I must restart the code (lot1.py) many times till tobi1=tobi2 that's normal,
    therefore maybe it's not possible to make a counter because when I restart a few times using execfile("lot1.py") the counter is always reset to 0

    And my biggest problem is when I restart ,so open the files 'appa.py' or 'appb.py' again it still shows the same first value.
    I know the values are changed in the files but the code don't accept to interpret and show these new values.
    without these values I can't do anything.
    A. I gave you solution for only the first problem. Not the second.
    B. Maybe it's a version thinggy, try running the code in 3.x
    EDIT: That's what I said:
    see, if you can debug it and get the absolute result:
    Last edited by Akshat1; May 28th, 2013 at 12:41 AM.
  8. #5
  9. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    19
    Rep Power
    0
    I have found the solution for the second problem.
    The module did'nt reload.
    The answer:
    import module
    reload(module)
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by Filip.V
    I have found the solution for the second problem.
    The module did'nt reload.
    The answer:
    import module
    reload(module)
    (Seems like you're using Python 2.x, just an FYI:
    For 3.x, you have to explicitly import a module called, 'imp' and then use the attribute reload to reload your module.)
    And what about the 1st one? Any thoughts?
  12. #7
  13. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    19
    Rep Power
    0
    Still not working in version 2.7
    Maybe I must ask a simpler question.
    Let's say I have the value appa
    fileName = 'app.py'
    I loop this 30 times
    Everytime I write the value appa it must numbering the files app.py from 1 to 30
    So the first time I write it must named app1.py
    The last time it must named app30.py
    How can I do that?
    Thanks
  14. #8
  15. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Location
    /dev/null
    Posts
    162
    Rep Power
    18
    Originally Posted by Filip.V
    Still not working in version 2.7
    Maybe I must ask a simpler question.
    Let's say I have the value appa
    fileName = 'app.py'
    I loop this 30 times
    Everytime I write the value appa it must numbering the files app.py from 1 to 30
    So the first time I write it must named app1.py
    The last time it must named app30.py
    How can I do that?
    Thanks
    Code:
    filename = 'app.py'
    appa = 'some_value_here_string_object'
    
    file = filename[:3]
    ext = filename[-2:]
    
    for i in range(30):
        out = open ('/path/to/' + file + str(i + 1) + '.' + ext, 'a')
        out.write(appa + '\n')
        out.close()
  16. #9
  17. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    19
    Rep Power
    0
    I can use this.
    Thanks noobie1000 and Akshat1
  18. #10
  19. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Here you go, the perfect solution:
    Code:
    tobi1 = 4
    tobi2 = 4
    
    appa = 'string1'
    appb = 'string2'
    
    if tobi1 == tobi2:
        for odd in range(1, 61, 2):
            fileName = 'app{0}.py'.format(str(odd))
            file1 = open(fileName,'w')
            file1.write(appa)
            file1.close()
        for even in range(2, 61, 2):
            fileName2 = 'app{0}.py'.format(str(even))
            file2 = open(fileName2, 'w')
            file2.write(appb)
            file2.close()
    print('Stop!')
  20. #11
  21. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    May 2013
    Posts
    19
    Rep Power
    0
    Yes this is the perfect solution,thanks
  22. #12
  23. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    100
    Rep Power
    2
    Originally Posted by Filip.V
    Yes this is the perfect solution,thanks
    Your welcome.

IMN logo majestic logo threadwatch logo seochat tools logo