Free style

Report a typo

In the code field, you can see a piece of a program. You don't have to know all the tools that are used in this code (you will if you continue studying Python)! For now, we provide an explanation of what it does:

  1. Import the module os that provides tools to work with the operating system of your computer.

  2. Ask a user for the input file name.

  3. If the path that a user has entered exists, do the following steps:

    1. Open the file in the reading mode, read its content.

    2. Apply the previously defined function process() to the text taken from the file.

    3. Open for writing a new file with a slightly changed name, write the processed text there.

    4. Print that everything is done.

  4. If the path entered by a user doesn't exist, print the corresponding message.

Your task is to put the comments in the code where you think they are appropriate. Don't change anything in the code, just add the comments. And mind the rules of good commenting!

Write a program in Python 3
import os

file_name = input("Please write the name of the file to work with:\n")

if os.path.exists(file_name):
with open(file_name) as file:
content = file.read()

new_content = process(content)

with open(f'{file_name}_processed.txt', 'w') as new_file:
new_file.write(new_content)

print("All done!")

else:
print("The file you entered does not exist!")
___

Create a free account to access the full topic