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

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    Output file using write command not working


    Hello,
    I asked about a the first part of this code in another thread, now I can excute it successfully however it is not running as expected.
    the code reads 1 file that contains 2 columns of numbers, does a mathematical operation on the numbers, and outputs them to another file. it is actually inspired by a code in a python scripting book.
    Code:
    import sys, math
    try:
        infile = input("enter full path of file1 "); outfile = input("enter same for file 2 ")
    except:
        print("please enter 2 file names"), sys.argv[0]; sys.exit(1)
    ifile = open(infile,'r')
    ofile = open(outfile, 'w')
    def Abdo(x):
        if x>0: return math.exp(x)
        else: return 0        
    for line in ifile:
        p=line.split()
        a = float(p[0]); x=float(p[1])
        fy=Abdo(x)
        print(fy);print(a)
        ofile.write('%g %12.5e \n' % (a,fy))
    ifile.close; ofile.close
    from that code I expect to get a new file on the path that I specify with the mathematical operation done on the numbers, however I get a file containing nothing, I tried several changes to the code with no good.

    The other important question I want to ask is about the presence of a way to troubleshoot the code line by line just like pressing F8 in visual basic.
    thank you
  2. #2
  3. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Okay, so it seems that you are using python3.

    Your program works for me:

    Code:
    $ echo "1 2" > a
    $ python3 test.py
    enter full path of file1 a
    enter same for file 2 b
    7.38905609893065
    1.0
    $ cat b
    1  7.38906e+00
  4. #3
  5. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480

    single step


    I'm sure idle has debugging modes with variable watch windows and good stuff like that.

    The pdb module handles single stepping. Here's an example of your program with the cruft* removed.
    Code:
    import math, pdb
    
    def Abdo(x):
        if 0 < x:
            return math.exp(x)
        return 0
    
    def loop_body(ifile,ofile):
        for line in ifile:
            p=line.split()
            #a = float(p[0]); x=float(p[1])   ################ see shorthand replacement on following line
            (a,x,) = map(float,p)
            fy=Abdo(x)
            print(fy);print(a)
            ofile.write('%g %12.5e \n' % (a,fy))
    
    def main(infile=None, outfile=None):
        if infile is None:
            infile = input("enter full path of file1 ")
        if outfile is None:
            outfile = input("enter same for file 2 ")
        with open(infile,'r') as ifile:
            with open(outfile, 'w') as ofile:
                pdb.set_trace()            #########debug trace turned on.  Read the documentations for instructions.
                loop_body(ifile,ofile)
    
    main()
    *Cruft is jargon for anything that is leftover, redundant and getting in the way.
    [code]Code tags[/code] are essential for python code and Makefiles!
  6. #4
  7. No Profile Picture
    Registered User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    5
    Rep Power
    0

    thanks for your replies


    thanks Partoj and b49P23TIvg for your replies, I am currently checking the modifications that b49P23TIvg did as well as the pdb module and will come back with questions about it if you don't mind.
    One other question, I could understand from Partoj reply that python 3 is not the most widely used version? am I correct? also in most books and web pages I find some problems implementing the code because probably everything is written in a previous version of python
  8. #5
  9. Contributing User
    Devshed Demi-God (4500 - 4999 posts)

    Join Date
    Aug 2011
    Posts
    4,709
    Rep Power
    480
    Much python 2 code exists. There are incompatible changes between python 2 and 3. The 2to3 program handles many of the changes to convert old programs to the new version.

    I expect the biggest changes you'll find in introductory tutorials are

    In python3:

    print 2 # print is a function. This is a SyntaxError
    print(2) # solution: use print like a function!

    input(prompt) # does what raw_input did.
    # raw_input was removed.
    [code]Code tags[/code] are essential for python code and Makefiles!
  10. #6
  11. No Profile Picture
    Contributing User
    Devshed Newbie (0 - 499 posts)

    Join Date
    Feb 2013
    Posts
    138
    Rep Power
    2
    Originally Posted by docaia
    [...]
    One other question, I could understand from Partoj reply that python 3 is not the most widely used version? am I correct? [...]
    That was not my intention to suggest Currently I'm only using python 2.5-2.7, so for me it was a bit exotic to see python 3 code.

IMN logo majestic logo threadwatch logo seochat tools logo