Skip to content

Instantly share code, notes, and snippets.

@LokeshKumarES
Last active December 13, 2022 04:13
Show Gist options
  • Save LokeshKumarES/646649e6ba7e6d966809e3f7dc14dc59 to your computer and use it in GitHub Desktop.
Save LokeshKumarES/646649e6ba7e6d966809e3f7dc14dc59 to your computer and use it in GitHub Desktop.
Write a Python program to reverse each word in file.
# text1.txt content
'''
hello easter science
how are you
we assist you to choose the best
'''
with open('test1.txt', 'r') as r_file:
x = r_file.readlines()
res = []
for i in x:
line_split = i.split()
y = list(reversed(line_split))
res.append(' '.join(y))
with open('text2.txt', 'w') as w_file:
for j in res:
w_file.write(j+'\n')
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment