1. Limited time only! Sign up for a free 30min personal tutor trial with Chegg Tutors
    Dismiss Notice
Dismiss Notice
Join Physics Forums Today!
The friendliest, high quality science and math community on the planet! Everyone who loves science is here!

Python and file output

  1. Nov 27, 2011 #1
    This isn't exactly part of my homework but I wanted to know how I could save the output of this program to txt file?

    The program was my homework and it was to allow the user to enter the starting values for a multiplication tables columns and rows and print a 10x10table

    I did it like so..

    Code (Text):

    print "To create a '10x10' multiplication table: "
    row = int(raw_input("Enter the first row number: "   ))
    col = int(raw_input("Enter the frist column number: "))

    lastRow = row + 10                          # These lines assign  the value  of the variables
    firstRow=col
    lastCol = col + 10

    print "   ",  

    while (col<lastCol):                # loop from col to col + 10
        print "%3d" % col,              #each iteration prints the column header value.
        col+=1                          
    print "\n","-"*50,
    col=firstRow                        #reset col values

     

    #end loop
    print  #moves to the next line

    while (row < lastRow):                      #This line creates the conditions in which both loops operate            
        print row,"|",                          #Prints a header for each row                                                
        while(col < lastCol):                   #This line creates the conditions for the inner loop
            print "%3d" %(col * row),          
            col +=1    

               

        col=firstRow                            #This line  updates the value for which "col"    
        row +=1                                  #This line updates the value for "row"
        print
     
    Is there a way I could save the printed values to a file?
     
  2. jcsd
  3. Nov 29, 2011 #2

    Mark44

    Staff: Mentor

  4. Nov 29, 2011 #3

    chroot

    User Avatar
    Staff Emeritus
    Science Advisor
    Gold Member

    mr.me, I will add that your code is very "un-Pythonic." It does not use any of the nifty features that make Python easier to use than other languages. Here's a more elegant version that uses implicit looping constructs:

    Code (Text):


    print "To create a '10x10' multiplication table: "
    firstRow = int(raw_input("Enter the first row number: "   ))
    firstCol = int(raw_input("Enter the frist column number: "))

    for row in range(firstRow, firstRow+10):
        cols = range(firstCol, firstCol+10)
        values = [ "%3d" % (row * col) for col in cols ]
        print "|".join(values), "\n", "-"*40
     
    - Warren
     
  5. Nov 30, 2011 #4

    NascentOxygen

    User Avatar

    Staff: Mentor

    You can run your program from the command line, something like this:
    python myprog.py

    Using the operating system, instead of modifying your program in any way, you can save a program's normal output to a file like this:
    python myprog.py > myresults.txt

    and the output will be saved on a file by that name in your current directory.
     
Know someone interested in this topic? Share this thread via Reddit, Google+, Twitter, or Facebook