
Data Structure
Networking
RDBMS
Operating System
Java
MS Excel
iOS
HTML
CSS
Android
Python
C Programming
C++
C#
MongoDB
MySQL
Javascript
PHP
- Selected Reading
- UPSC IAS Exams Notes
- Developer's Best Practices
- Questions and Answers
- Effective Resume Writing
- HR Interview Questions
- Computer Glossary
- Who is Who
Save File with User-defined Name in Python
In Python, saving files with user?defined file names is a common requirement in various applications and projects. By allowing users to specify the file name, we can provide them with a more personalized and customizable experience. This article will explain the process of saving files with file names provided by the user, using Python.
Algorithm
A general algorithm for saving file with file name from user is as follows:
Prompt the user to enter the desired file name.
Check if the file name ends with the desired file extension. If not, append it.
Create a file object using the open() function with the desired file name and the 'w' mode.
Prompt the user to enter the content to be written to the file.
Write the content to the file using the write() method of the file object
Close the file using the close() method.
Handle any exceptions that may occur during the file creation and writing process.
Method 1:Using the open() function and write() method
This method utilizes the built?in open() function in Python to create a file object, and then uses the write() method of the file object to write content to the file
Syntax
file = open(file_name, mode)
Here, file = open(file_name, mode) creates a file object named file by opening the file specified by file_name in the given mode. The mode parameter determines the purpose for which the file is opened, such as 'w' for write mode.
file.write(content)
Here, file.write(content) is used to write the specified "content" into the file referenced by the "file" variable.
Example
In the below example, The open() function is used to create a file object named file in write mode ('w') with the provided file name. The user is prompted to enter the content to be written to the file. The write() method is used to write the content to the file. The close() method is called to close the file and release system resources. Finally, a success message is printed indicating that the file has been saved successfully
file_name = input("Enter the desired file name: ") file = open(file_name, 'w') content = input("Enter the content to be written to the file: ") file.write(content) file.close() print("File saved successfully!")
Output
Enter the desired file name: my_file.txt Enter the content to be written to the file: This is the content of my file. File saved successfully!

The above image shows that the file is successfully saved by the same filename as entered by you in the prompt. I.e my_file.txt. You can give any file name of your choice.
Method 2:Using the input() function and write() method
This method involves using the input() function to get the desired file name and content from the user, and then using the write() method to write the content to the file.
Syntax
input(prompt)
Here, input is the built?in function used to accept user input from the console. The prompt is an optional parameter that specifies the message or prompt to be displayed to the user before they enter their input.
Example
In the below example, The open() function is used in a with statement to automatically handle file closing. The file is opened in write mode ('w') using the provided file name. The write() method is used to write the content to the file. After the with block, the file is automatically closed. Finally, a success message is printed indicating that the file has been saved successfully.
file_name = input("Enter the desired file name: ") content = input("Enter the content to be written to the file: ") with open(file_name, 'w') as file: file.write(content) print("File saved successfully!")
Output
Enter the desired file name: my_file.txt Enter the content to be written to the file: This is the content of my file. File saved successfully!

As we entered the file name as my_file.txt in the prompt. The file is saved successfully as my_file.txt in the current directory.
Method 3:Using the Pathlib Module
The pathlib module in Python provides an object?oriented interface for working with files and directories. It allows us to create a file using the open() function and write content to it.
Syntax
from pathlib import Path
Here, we import the pathlib module from the Path library of Python. We can specify the module name that we want to import from the particular library.
path_variable = Path(path_string)
Here, path_string is a string representing the file or directory path. The Path() function creates a Path object using the specified path_string. The Path object provides various methods and attributes for working with file paths, such as reading, writing, or manipulating paths.
Example
In the below example, the pathlib module is imported to use its functionality for working with file paths. The user is prompted to enter the desired file name, including the file extension.
The user is prompted to enter the content to be written to the file. The file path is created using the Path() function, passing the file name as an argument. The write_text() method is called on the file_path object to write the content to the file. Finally, a success message is printed indicating that the file has been saved successfully.
from pathlib import Path file_name = input("Enter the desired file name: ") content = input("Enter the content to be written to the file: ") file_path = Path(file_name) file_path.write_text(content) print("File saved successfully!")
Output
Enter the desired file name: my_file.txt Enter the content to be written to the file: This is the content of my file. File saved successfully!

The above image shows that the file is successfully saved as my_file.txt file in the current directory. If you open the file the content of the file will contain the text you entered in the file.
Method 4:Using the io Module
The io module in Python provides classes for working with streams, including file?like objects. We can utilize the open() function from this module to create a file object and write content to it.
Syntax
file_variable = io.open(file_name, mode, encoding)
Here, file_name is a string representing the file name or path. mode is an optional parameter that specifies the mode in which the file should be opened, such as 'r' for reading, 'w' for writing, 'a' for appending, etc. encoding is an optional parameter that specifies the encoding to be used when reading or writing the file.
Example
In the below example, the io module is imported to use its functionality for working with file?like objects. The user is prompted to enter the desired file name, including the file extension.
The user is prompted to enter the content to be written to the file. The open() function from the io module is used in a with statement to automatically handle file closing. The file is opened in write mode ('w') using the provided file name. The write() method is used to write the content to the file. After the with block, the file is automatically closed. Finally, a success message is printed indicating that the file has been saved successfully.
import io file_name = input("Enter the desired file name: ") content = input("Enter the content to be written to the file: ") with io.open(file_name, 'w') as file: file.write(content) print("File saved successfully!")
Output
Enter the desired file name: my_file.txt Enter the content to be written to the file: This is the content of my file. File saved successfully!

The above image clearly shows that the file is saved as my_file.txt file name as entered in the prompt. If you open the file the content of the file will contain the text you entered in the file.
Conclusion
In this article, we discussed how we can save files with user?defined file names from the user using Python. By allowing users to specify the file name, we can create a more interactive and personalized experience in our Python applications. Remember to handle exceptions appropriately to ensure the code operates smoothly.