Python Print to File
Tags: python, programming, codenewbie, tutorial
Published: 2021-12-08 14:13:07

---
title: Python Print to File
published: true
date: 2021-12-05 19:31:44 UTC
tags: #python #programming #codenewbie #tutorial
canonical_url: https://itsmycode.com/python-print-to-file/
---

ItsMyCode |

We always use print statements in [Python](https://www.w3basic.com/python/) to display the output in the console or command line terminal. However, sometimes we want to change this behavior to print to a text file instead of the console.

## Print to file using file argument

The **`print()`** method accepts the file parameter as one of the arguments, and using this, we can print the standard output to a file.

The default value of the file argument is **`sys.stdout`** , which prints the output on the screen.

**Example 1: Print to the text file**

The below program opens the [existing text file](https://itsmycode.com/how-to-check-if-a-file-exists-in-python/) in the write mode using the **`open()`** function (if file is not available it will [create a new file](https://itsmycode.com/python-write-text-file/)) and writes all the text specified inside the print statement. Once the content is written to a file, we close the file using the **`close()`** method.

```python 
# Print to the text file
file = open('log.txt', 'w')
print('This is a sample print statement', file = file)
print('Hello World', file = file)

file.close()
```

**Output**

```python 
This is a sample print statement
Hello World

```

**Example 2: Print to the text file using inline file argument**

If you want to control only a few print statements to output to a **[text file](https://itsmycode.com/python-read-text-file/)**, you could inline the file argument inside the print statement, as shown below.

```python 
# Appends the print statement into a log.txt

print("Welcome to ItsMyCode", file=open('log.txt','a'))

```

**Output**

```python 
Welcome to ItsMyCode
```

## Redirecting the Standard Output Stream to a file

Specifying the file argument helps debug the smaller programs but is not desirable in some situations. 

In this case, we can redirect all the standard output stream to a file. Setting the standard output to a file ensures that the text specified inside the **`print()`** function will be written to a file instead of displaying in the console. 

The standard output can be set in Python by importing the **`sys `** module and setting the **_`stdout`_** to a file or file-like object.

```python 
import sys

# Prints in a console/terminal
print('Default mode: Prints on a console.')

# Store the reference of original standard output into variable
original_stdout = sys.stdout 

# Create or open an existing file in write mode
with open('log.txt', 'w') as file:
    # Set the stdout to file object
    sys.stdout = file
    print('File Mode: Print text to a file.')
    
    # Set the stdout back to the original or default mode
    sys.stdout = original_stdout

print('Default Mode: Prints again on a console.')

```

**Output**

```python 
Hello Welcome to Python Tutorials !!!
File Mode: Print text to a file.


Default mode: Prints on a console.
Default Mode: Prints again on a console.
```

## Redirect the Python Script Output to File

The easiest and dirty way is to redirect the Python script’s output directly from the command line window while executing the script. 

For example, if you have a Python script file named **`main.py`** with the following code.

We can redirect the output of the Python script using the _ **right angle bracket** _, as shown below.

```python 
python3 main.py > output.txt  

print("Hello World !!!")

```

If you open the `output.txt` file, you can see the below content written into the text file.

```
Hello World !!!
```

The post [Python Print to File](https://itsmycode.com/python-print-to-file/) appeared first on [ItsMyCode](https://itsmycode.com).