Skip to main content
added 2 characters in body
Source Link
Levon
  • 144k
  • 35
  • 205
  • 194

Output to the console comes only via stdout and stderr as far as I know.

Do you want to print to a file, then you can do this

 with open('outfile.txt', 'w') as f: f.write('this is a test') 

or alternatively if you prefer using print:

 print >> f, 'this is a test' 

In the above example a file named outfile.txt is opened for write access. This means any data already present will be wiped out by new data written to it. If you wanted to append you'd open the file in a mode. See the documentation for open() for more information.

Files that are opened need to be closed, using the with construct takes care of that for you (and also closes the file in case you encounter an exception).

Output to the console comes only via stdout and stderr as far as I know.

Do you want to print to a file, then you can do this

 with open('outfile.txt', 'w') as f: f.write('this is a test') 

or alternatively if you prefer using print:

 print >> f, 'this is a test' 

In the above example a file named outfile.txt is opened for write access. This means any data already present will be wiped out by new data written to it. If you wanted to append you'd open the file in a mode. See the documentation for open for more information.

Files that are opened need to be closed, using the with construct takes care of that for you (and also closes the file in case you encounter an exception).

Output to the console comes only via stdout and stderr as far as I know.

Do you want to print to a file, then you can do this

 with open('outfile.txt', 'w') as f: f.write('this is a test') 

or alternatively if you prefer using print:

 print >> f, 'this is a test' 

In the above example a file named outfile.txt is opened for write access. This means any data already present will be wiped out by new data written to it. If you wanted to append you'd open the file in a mode. See the documentation for open() for more information.

Files that are opened need to be closed, using the with construct takes care of that for you (and also closes the file in case you encounter an exception).

Source Link
Levon
  • 144k
  • 35
  • 205
  • 194

Output to the console comes only via stdout and stderr as far as I know.

Do you want to print to a file, then you can do this

 with open('outfile.txt', 'w') as f: f.write('this is a test') 

or alternatively if you prefer using print:

 print >> f, 'this is a test' 

In the above example a file named outfile.txt is opened for write access. This means any data already present will be wiped out by new data written to it. If you wanted to append you'd open the file in a mode. See the documentation for open for more information.

Files that are opened need to be closed, using the with construct takes care of that for you (and also closes the file in case you encounter an exception).