4. Create new files and update contenct

۰ نظر گزارش تخلف
Farnaz Baghban
Farnaz Baghban

#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()
========================================

نظرات

در حال حاضر امکان درج نظر برای این ویدیو غیرفعال است.

توضیحات

4. Create new files and update contenct

۰ لایک
۰ نظر

#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()
========================================