Files in Python:
Files are identified locations on the disc where associated data is stored. They’re used to keep data in non-volatile memory for a long time (e.g. hard disk).
We use files for future usage of the data by permanently saving it because Random Access Memory (RAM) is volatile (it loses its contents when the machine is turned off).
We must first open a file before we can read from or write to it. When we’re finished, it needs to be closed so that the file’s resources may be released.
As a result, a file operation in Python is performed in the following order:
- Create a new file
- You can either read or write (perform the operations)
- Close the file.
Given a file, the task is to reverse each word in the given file in python
- Python Program to Count Words in a Text File those are Ending with Alphabet “e”
- Python Program to Capitalize the First Letter of Every Word in the File
- Python Program to Reverse the Content of Text File and Store it in Another File
Program to Reverse Each Word in a Text File in Python
Below is the full approach for reversing each word in the given file in 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.
- Read the above file using the read() function(get the content) and store it in a variable.
- Take a variable and initialize it with a new empty string.
- Split the given data into a list of words using the split() function and iterate in that list using the for loop.
- Reverse each word and store it in a variable.
- Add the above-reversed word to the above empty result string and store it in the same variable.
- Give spaces in the result string.
- Open the file in write mode. In this case, we’re writing the contents into the file.
- Write the data stored in the result string into the file using the write() function.
- The Exit of 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: # Read the above file using the read() function(get the content) and store it in a variable gvn_data = givenfilecontent.read() # Take a variable and initialize it with a new empty string rslt_str = "" # Split the given data into list of words using the split() function and # iterate in that list using the for loop for wrd in gvn_data.split(): # Reverse each word and store it in a variable revrse_wrd = wrd[::-1] # Add the above reversed word to the above empty result string and # store it in the same variable. rslt_str += revrse_wrd # Give spaces in the result string rslt_str += " " # Open the file in write mode. In this case, we're writing the contents into the file. with open("samplefile.txt","w") as givenfilecontent: # write the data stored in the result string into the file using the write() function givenfilecontent.write(rslt_str)
Output:
olleH siht si skeeghcetB
File Content: (Before Reversing)
Hello this is Btechgeeks
File Content: (After Reversing)
olleH siht si skeeghcetB
Google Colab Images:
Files and Code: