How to save user input to a File in Python

avatar
Borislav Hadzhiev

Last updated: Apr 9, 2024
4 min

banner

# Save user input to a File in Python

To save user input to a file:

  1. Use the with open() statement to open the file in write mode.
  2. Use the input() function to take input from the user.
  3. Use the file.write() method to write the input to the file.
  4. The with statement automatically closes the file.
main.py
# ✅ Save user input to file file_name = 'example.txt' with open(file_name, 'w', encoding='utf-8') as my_file: my_file.write(input('Your message: ')) # ------------------------------------------------- # ✅ Take a filename and contents of a file from user input file_name = input('Filename with extension, e.g. example.txt: ') with open(file_name, 'w', encoding='utf-8') as my_file: my_file.write(input('Your message: '))

save input to file

The code for this article is available on GitHub
The with open() syntax takes care of automatically closing the file even if an exception is raised.

The first example writes user input to a file named example.txt and the second example prompts the user for the filename.

main.py
file_name = 'example.txt' with open(file_name, 'w', encoding='utf-8') as f: f.write(input('Your message: '))

The open() function opens a file and returns the corresponding file object.

You will most commonly pass the following 3 parameters to the open function:

NameDescription
filenameThe name of the file
modeThe mode in which the file is opened, e.g. r for reading or w for writing (it defaults to r)
encodingWhat encoding to use to decode or encode the file (default is platform-dependent)

We used the write() method on the file object to write the user input to the file.

The w (write) mode truncates the file and opens it for writing.

# Using the a mode to append to a file

If you need to append to the file, you can use the a (append) mode.

main.py
file_name = 'example.txt' with open(file_name, 'a+', encoding='utf-8') as my_file: my_file.write(input('Your message: ') + '\n')

using the a mode to append to a file

The code for this article is available on GitHub
The a+ mode opens the file for reading and appending. The file is created if it doesn't exist.

I added a newline character at the end of the user input, so it starts on a new line every time, but you don't have to.

# Writing a message from user input to a specified file

The second example takes the filename and the contents of the file from user input.

main.py
file_name = input('Filename with extension, e.g. example.txt: ') with open(file_name, 'w', encoding='utf-8') as f: f.write(input('Your message: '))

writing message from user input to specified file

The code for this article is available on GitHub

The filename should contain the extension, e.g. example.txt.

The input() function takes an optional prompt argument and writes it to standard output without a trailing newline.

You might also see the older, more manual syntax with an explicit call to the close() method.

main.py
file_name = 'example.txt' f = open(file_name, 'w', encoding='utf-8') f.write(input('Your message: ')) f.close()

You should generally avoid this syntax because it requires us to manually close the file after writing to it.

We also have to handle any errors because the call to the close() method won't run if an error occurs before that.

It's a best practice to use the with statement as it automatically takes care of closing the file even if an error is raised.

main.py
file_name = 'example.txt' with open(file_name, 'w', encoding='utf-8') as my_file: my_file.write(input('Your message: '))
The code for this article is available on GitHub

When using the open() syntax without the if statement, you might get a memory leak if unhandled exception is raised.

I've also written an article on how to take a file path from user input.

# Additional Resources

You can learn more about the related topics by checking out the following tutorials:

I wrote a book in which I share everything I know about how to become a better, more efficient programmer.
book cover
You can use the search field on my Home Page to filter through all of my articles.