سرگرمی و طنز ورزشی کارتون و انیمیشن علم و فن آوری خودرو و وسایل نقلیه آموزش موسیقی و هنر اخبار و سیاست حیوانات و طبیعت بازی حوادث مذهبی
۱۴۰۵/۰۳/۱۶
#9 Python Programming Exercise

Construct an application that will open a file with arbitrary text (arbitrary number of lines). It reverses the text (not line by line but the whole text) and write this reversed text into another file. Let's see an example:

input_file.txt

This is an example
for the input file!
output_file.txt

!elif tupni eht rof
elpmaxe na si sihT
As you can see the lines have been changed as well as the strings have been reversed!

Good luck!
==========================================
#9 Python Programming Solution

# these are the names of the files. The input is an existing file!
file_input = open('original_file.txt', 'r')
file_output = open('file_reversed', 'a')

# you can use the reversed() built in function + [::-1] is related to string slicing
for line in reversed(list(file_input)):
reversed_line = line[::-1]
file_output.write(reversed_line)

# do not forget to close the files
file_input.close()
file_output.close()
========================================