{"id":9999,"date":"2021-09-30T14:00:59","date_gmt":"2021-09-30T08:30:59","guid":{"rendered":"https:\/\/python-programs.com\/?p=9999"},"modified":"2021-11-22T18:34:34","modified_gmt":"2021-11-22T13:04:34","slug":"python-program-to-count-the-number-of-blank-spaces-in-a-text-file","status":"publish","type":"post","link":"https:\/\/python-programs.com\/python-program-to-count-the-number-of-blank-spaces-in-a-text-file\/","title":{"rendered":"Python Program to Count the Number of Blank Spaces in a Text File"},"content":{"rendered":"
Don’t miss the chance of Java programs examples with output pdf free download<\/a> as it is very essential for all beginners to experienced programmers for cracking the interviews.<\/p>\n Files in Python:<\/strong><\/p>\n Python File handling is a method that allows you to save the program’s output to a file or read data from a file. In the programming world, file handling is a critical notion. File management is employed in practically every type of project. For example, suppose you’re developing an inventory management system. You have data connected to sales and purchases in the inventory management system, thus you must save that data somewhere. You can save that data to a file using Python file management. If you want to undertake data analysis, you must be given data in the form of a comma-separated file or a Microsoft Excel file. You can read data from a file and also store output back into it using file handling.<\/p>\n Given the file, the task is to count the number of blank spaces in a Text File.<\/p>\n Below is the full approach to calculate the total number of Blank Spaces in the given Text 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 Image:<\/strong><\/p>\n <\/p>\n Hello.txt<\/strong><\/p>\n <\/p>\n Related Programs<\/strong>:<\/p>\n <\/p>\n","protected":false},"excerpt":{"rendered":" Don’t miss the chance of Java programs examples with output pdf free download as it is very essential for all beginners to experienced programmers for cracking the interviews. Files in Python: Python File handling is a method that allows you to save the program’s output to a file or read data from a file. In …<\/p>\nProgram to Count the Number of Blank Spaces in a Text File<\/h2>\n
\n
# Take a variable say blankCount that stores the calculate the\r\n# total number of blank spaces in a given file and initialize it to 0.\r\nblankCount = 0\r\n# Enter the file name of the file using the input() function and store it in a variable.\r\nfilename = input(\"Enter the file name = \")\r\n# In read mode, open the file with the entered file name.\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 # Split the line into words using the split() function.\r\n wordslist = fileline.split()\r\n # To traverse the words in the list, use a for loop, then another\r\n # for loop to traverse the letters in the word.\r\n for words in wordslist:\r\n for char in words:\r\n # Check to see if the letter is a space using isspace,\r\n # and if so, increase the blank count\u00a0by 1.\r\n if(char.isspace):\r\n blankCount = blankCount+1\r\nprint('The total count of the blank spaces in the given file = ', blankCount)\r\n<\/pre>\n
Enter the file name = hello.txt\r\nThe total count of the blank spaces in the given file = 21<\/pre>\n
\n
\n