Save Output of Python Program to TXT File: Tips & Tricks

  • Context: Python 
  • Thread starter Thread starter mr.me
  • Start date Start date
  • Tags Tags
    File Output Python
Click For Summary

Discussion Overview

The discussion revolves around methods for saving the output of a Python program to a text file. The context includes both a specific homework assignment involving multiplication tables and general techniques for file output in Python.

Discussion Character

  • Technical explanation
  • Conceptual clarification
  • Homework-related

Main Points Raised

  • One participant seeks advice on how to save the output of their multiplication table program to a text file.
  • Another participant references the Python documentation on reading and writing files as a resource for file output techniques.
  • A third participant critiques the original code for being "un-Pythonic" and provides a more elegant version of the multiplication table program using list comprehensions and implicit looping constructs.
  • A different participant suggests running the program from the command line and redirecting the output to a file using the '>' operator, explaining that this method does not require modifying the original program.

Areas of Agreement / Disagreement

Participants present various methods for saving output, but there is no consensus on a single best approach. Different perspectives on coding style and efficiency are also evident.

Contextual Notes

Some responses assume familiarity with command line operations and Python syntax, which may not be universally understood by all participants.

mr.me
Messages
49
Reaction score
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?
 
Technology news on Phys.org
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
 
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.
 

Similar threads

  • · Replies 5 ·
Replies
5
Views
7K
  • · Replies 2 ·
Replies
2
Views
2K
Replies
3
Views
3K
  • · Replies 9 ·
Replies
9
Views
4K
  • · Replies 7 ·
Replies
7
Views
5K
  • · Replies 4 ·
Replies
4
Views
3K
  • · Replies 8 ·
Replies
8
Views
2K
  • · Replies 17 ·
Replies
17
Views
3K
  • · Replies 4 ·
Replies
4
Views
12K
  • · Replies 1 ·
Replies
1
Views
3K