Read word by word python – Python Program to Read File Word by Word

Files In Python:

Read word by word python: A file is a piece of data or information stored on a computer’s hard drive. You’re already familiar with a variety of file kinds, including music, video, and text files. Manipulation of these files is trivial with Python. Text files and binary files are the two types of files that are commonly used. Binary files contain binary data that can only be read by a computer, whereas text files include plain text.

For programmers and automation testers, Python file handling (also known as File I/O) is a crucial topic. Working with files is required in order to write to or read data from them.

In addition, if you didn’t know, I/O activities are the most expensive techniques via which software might fail. As a result, when implementing file processing for reporting or any other reason, you should proceed with caution. The construction of a high-performance application or a robust solution for automated software testing can benefit from optimizing a single file activity.

Given a file, the task is to print all words in the given file using python.

Program to Read File Word by Word using Python

Approach:

  • Make a single variable to store the path of the file. This is a constant value. This value must be replaced with the file path from your own system in the example below.
  • Open the file in read-only mode. In this case, we’re simply reading the contents of the file.
  • Iterate through the lines of the file using the For loop.
  • Split the words of the line using the split() function and store them in a variable(it is of type list).
  • Loop in the above list using another Nested For loop and print all the contents of the list using the print() function.
  • The Exit of the Program.

Below is the implementation:

# Make a single variable to store the path of the file. This is a constant value.
# This value must be replaced with the file path from your own system in the example below.
givenFilename = "samplefile.txt"
# Open the file in read-only mode. In this case, we're simply reading the contents of the file.
with open(givenFilename, 'r') as givenfilecontent:
    # Iterate through the lines of the file using the For loop.
    print('The words in the given file : ')
    for gvnfileline in givenfilecontent:
      # Split the words of the line using the split() function and store them in a variable(it is of type list).
        gvnfilewords = gvnfileline.split()
        # Loop in the above list using another Nested For loop
        # and print all the contents of the list using the print() function.
        for words in gvnfilewords:
            print(words)

Output:

The words in the given file : 
hello
this
is
btechgeeks
Good
morning
this
is
btechgeeks
By
now
you
might
be
aware
that
Python
is
a
Popular
Programming
Language
used
right
from
web
developers
to
data
scientists.
Wondering
what
exactly
Python
looks
like
and
how
it
works?
The
best
way
to
learn
the
language
is
by
practicing.
BTech
Geeks
have
listed
a
wide
collection
of
Python
Programming
Examples.

Explanation:

  • The file path is stored in the variable ‘file name.’ Change the value of this variable to the path of your own file.
  • Dragging and dropping a file onto the terminal will show its path. The code will not run unless you change the value of this variable.
  • The file will be opened in reading mode. Use the open() function to open a file. The path to the file is the method’s first parameter, and the mode to open the file is the method’s second parameter.
  • When we open the file, we use the character ‘r’ to signify read-mode.
  • The split() method separates all of the words in the given file, which we then print word by word.

samplefile.txt

hello this is btechgeeks
Good morning this is btechgeeks By now you might be aware that Python is a Popular Programming Language used right from web developers to data scientists.
Wondering what exactly Python looks like and how it works? The best way to learn the language is by practicing. 
BTech Geeks have listed a wide collection of Python Programming Examples.

Sample Implementation in google colab: