• Support PF! Buy your school textbooks, materials and every day products Here!

Python and file output

  • Comp Sci
  • Thread starter mr.me
  • Start date
  • #1
49
0
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:
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?
 

Answers and Replies

  • #3
chroot
Staff Emeritus
Science Advisor
Gold Member
10,226
34
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:
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
 
  • #4
NascentOxygen
Staff Emeritus
Science Advisor
9,244
1,071
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.
 

Related Threads for: Python and file output

Replies
0
Views
1K
Replies
5
Views
38K
Replies
31
Views
10K
  • Last Post
Replies
1
Views
287
Replies
1
Views
1K
Replies
27
Views
21K
Replies
6
Views
1K
Top