Last updated: Apr 9, 2024
Reading time·4 min
To save user input to a file:
with open()
statement to open the file in write mode.input()
function to take input from the user.file.write()
method to write the input to the file.with
statement automatically closes the file.# ✅ 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: '))
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.
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:
Name | Description |
---|---|
filename | The name of the file |
mode | The mode in which the file is opened, e.g. r for reading or w for writing (it defaults to r ) |
encoding | What 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.
a
mode to append to a fileIf you need to append to the file, you can use the a
(append) mode.
file_name = 'example.txt' with open(file_name, 'a+', encoding='utf-8') as my_file: my_file.write(input('Your message: ') + '\n')
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.
The second example takes the filename and the contents of the file from user input.
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: '))
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.
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.
file_name = 'example.txt' with open(file_name, 'w', encoding='utf-8') as my_file: my_file.write(input('Your message: '))
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.
You can learn more about the related topics by checking out the following tutorials: