Open In App

Take input from user and store in .txt file in Python

Last Updated : 07 Nov, 2022
Improve
Improve
Like Article
Like
Save
Share
Report

In this article, we will see how to take input from users and store it in a .txt file in Python. To do this we will use python open() function to open any file and store data in the file, we put all the code in Python try-except block. Let’s see the implementation below.

Stepwise Implementation 

Step 1: First, we will take the data from the user and store it in a temporary variable ‘temp’.

Step 2: We will open the file with the file name using the Python open() function with the write mode enabled.

Note: If the file doesn’t exist it will automatically create the file for us.

# this means the file will open
# with write mode enabled
x = open(gfg.txt,w)

Step 3: Now we will write the data into the file we just created.

x.write(temp)

Step 4: Finally, we will keep the file operations under a try-except block to check for exceptions.

Example 1:

Python3




temp = input("Please enter your information!!   ")
try:
    with open('gfg.txt', 'w') as gfg:
        gfg.write(temp)
except Exception as e:
    print("There is a Problem", str(e))


Output:

 

gfg.txt

Example 2:

Python3




temp = input("Please enter your information!!   ")
try:
    with open('gfg.txt', 'w') as gfg:
        gfg.write(temp)
except Exception as e:
    print("There is a Problem", str(e))


Output:

input text

gfg.txt


Previous Article
Next Article

Similar Reads

Take Matrix input from user in Python
Matrix is nothing but a rectangular arrangement of data or numbers. In other words, it is a rectangular array of data or numbers. The horizontal entries in a matrix are called as 'rows' while the vertical entries are called as 'columns'. If a matrix has r number of rows and c number of columns then the order of matrix is given by r x c. Each entrie
3 min read
Find line number of a specific string or substring or word from a .txt file in Python
Finding the line number of a specific string and its substring is a common operation performed by text editors or any application with some level of text processing capabilities. In this article, you will learn how to find line number of a specific string or substring or word from a .txt (plain text) file using Python. The problem in hand could be
4 min read
Change case of all characters in a .txt file using Python
In this article, we will learn how to change the case of all characters present in a text file using Python. We will use Python methods Python upper() to change all characters to upper case, and Python lower() to change all characters to lower case. For example, If we have text file data.txt as shown below. Change all characters to uppercase from t
3 min read
Install Packages Using PIP With requirements.txt File in Python
Installing more than one package in Python simultaneously is a common requirement for any user migrating between operating systems. Most users are unaware of the availability of batch installing all the packages. They default to manually installing each library one after the other, which is time-consuming. This article will teach you how to install
2 min read
How to Create Requirements.txt File in Python
Creating and maintaining a requirements.txt file is a fundamental best practice for Python development. It ensures that your project's dependencies are well-documented and easily reproducible, making it easier for others to work on your code and reducing the likelihood of compatibility issues. Create Requirements.txt File in PythonWhen working on P
2 min read
Print the Content of a Txt File in Python
Python provides a straightforward way to read and print the contents of a .txt file. Whether you are a beginner or an experienced developer, understanding how to work with file operations in Python is essential. In this article, we will explore some simple code examples to help you print the content of a .txt file using Python. How To Print The Con
3 min read
Convert PDF to TXT File Using Python
As the modern world gets digitalized, it is more and more necessary to extract text from PDF documents for purposes such as data analysis or content processing. There is a versatile ecosystem of Python libraries that can work with different file formats including PDFs. In this article, we will show how to build a simple PDF-to-text converter in Pyt
2 min read
Python program to reverse the content of a file and store it in another file
Given a text file. The task is to reverse as well as stores the content from an input file to an output file. This reversing can be performed in two types. Full reversing: In this type of reversing all the content gets reversed. Word to word reversing: In this kind of reversing the last word comes first and the first word goes to the last position.
2 min read
Convert TSV to TXT in Python
In this article, we are going to see how to convert TSV files to text files in Python. Approach:Open TSV file using open() functionOpen txt file in which we are going to write TSV file dataThen use csv.reader() it will return a reader object which will iterate over lines in the given TSV file. (set delimiter="\t")Write data in the opened txt file l
2 min read
Find all the Files in a Directory with .txt Extension in Python
Directory traversal is a common operation performed by file locator in Operating Systems. The operating system offers elaborate methods for streamlining the search process and the choice to search for a selected filename/extension. This article will teach you how to find all the files in a directory with a .txt (text files) extension using Python.
5 min read
three90RightbarBannerImg