{"id":9788,"date":"2021-09-30T14:00:23","date_gmt":"2021-09-30T08:30:23","guid":{"rendered":"https:\/\/python-programs.com\/?p=9788"},"modified":"2021-11-22T18:34:36","modified_gmt":"2021-11-22T13:04:36","slug":"python-program-to-read-a-string-from-the-user-and-append-it-into-a-file","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-read-a-string-from-the-user-and-append-it-into-a-file\/","title":{"rendered":"Python Program to Read a String from the User and Append it into a File"},"content":{"rendered":"
Our website provided core java programs examples with output<\/a> aid beginners and expert coders to test their knowledge gap and learn accordingly.<\/p>\n Files in Python:<\/strong><\/p>\n 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.<\/p>\n 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.<\/p>\n 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.<\/p>\n Given a string and file, the task is to append this string to the given file.<\/p>\n Below is the full process Read a string from the user and append it to the file.<\/p>\n Approach:<\/strong><\/p>\n Below is the implementation:<\/strong><\/p>\n Output:<\/strong><\/p>\n Explanation:<\/strong><\/p>\n Google Colab Images:<\/strong><\/p>\n Files and Code:<\/strong><\/p>\n <\/p>\n Code:<\/strong><\/p>\n <\/p>\n Output:<\/strong><\/p>\n <\/p>\n Samplefile2.txt<\/strong><\/p>\n 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 …<\/p>\nProgram to Read a String from the User and Append it into a File<\/h2>\n
\n
#Give the string as static input and store it in a variable.\r\ngivenstring=input('Enter some random string = ')\r\n#Enter the file name of the file using the input() function and store it in a variable.\r\nfilename = input(\"Enter the first file name = \")\r\n#In append mode, open the first file with the entered file name.\r\ngivenfile=open(filename,\"a\")\r\n#Write a new line character to the given file using the write() function and '\\n'.\r\ngivenfile.write(\"\\n\")\r\n#Append the given string to the given file using the write() function.\r\ngivenfile.write(givenstring)\r\n#Close the given file using close() function.\r\ngivenfile.close()\r\nprint('Printing the contents of the given file :')\r\nwith open(filename, 'r') as givenfile:\r\n #Using for loop, go over the lines in the first file.\r\n for fileline in givenfile:\r\n #print the lines\r\n print(fileline)\r\n<\/pre>\n
Enter some random string = btechgeeks\r\nEnter the first file name = samplefile2.txt\r\nPrinting the contents of the given file :\r\nhello this is\r\n\r\nbtechgeeks<\/pre>\n
\n
\nRelated Programs<\/strong>:<\/p>\n\n