Python Program to Read a String from the User and Append it into a File

Our website provided core java programs examples with output aid beginners and expert coders to test their knowledge gap and learn accordingly.

Files in Python:

A file is a piece of information or data that is kept in computer storage devices. You are already familiar with several types of files, such as music files, video files, and text files. Python makes it simple to manipulate these files. In general, files are classified into two types: text files and binary files. Text files are plain text, whereas binary files include binary data that can only be read by a computer.

Python file handling (also known as File I/O) is an important topic for programmers and automation testers. It is necessary to work with files to either write to or read data from them.

Furthermore, if you are not already aware, I/O operations are the most expensive procedures through which a program might fail. As a result, you should take caution while implementing file handling for reporting or any other reason. Optimizing a single file activity can contribute to the development of a high-performance application or a solid solution for automated software testing.

Given a string and file, the task is to append this string to the given file.

Program to Read a String from the User and Append it into a File

Below is the full process Read a string from the user and append it to the file.

Approach:

  • Give the string as static input and store it in a variable.
  • Give the file name as user input and store it in a variable.
  • Open the file with the given file name in append mode.
  • Write a new line character to the given file using the write() function and ‘\n’.
  • Append the given string to the given file using the write() function.
  • Close the given file using close() function.
  • To print the contents of the given file after appending the string we open the file again in read mode.
  • Print the contents of the given file using for loop
  • The exit of the program.

Below is the implementation:

#Give the string as static input and store it in a variable.
givenstring=input('Enter some random string = ')
#Enter the file name of the  file using the input() function and store it in a variable.
filename = input("Enter the first file name = ")
#In append mode, open the first file with the entered file name.
givenfile=open(filename,"a")
#Write a new line character to the given file using the write() function and '\n'.
givenfile.write("\n")
#Append the given string to the given file using the write() function.
givenfile.write(givenstring)
#Close the given file using close() function.
givenfile.close()
print('Printing the contents of the given file :')
with open(filename, 'r') as givenfile:
  #Using for loop, go over the lines in the first file.
    for fileline in givenfile:
      #print the lines
        print(fileline)

Output:

Enter some random string = btechgeeks
Enter the first file name = samplefile2.txt
Printing the contents of the given file :
hello this is

btechgeeks

Explanation:

  • A file name must be entered by the user.
  • The file is opened in append mode with the open() function.
  • The user’s string is retrieved, appended to the existing file, and the file is closed.
  • The file is opened in read mode once more.
  • We printed the contents of the given file using For loop by traversing through the lines.

Google Colab Images:

Files and Code:

Code:

Output:

Samplefile2.txt


Related Programs: