A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://realpython.com/lessons/functions-input-and-output/ below:

Input and Output (Video) – Real Python

Hello!

At 4:40, why is it important that we ensure the file is closed after we open it in read mode?

This is the explanation from the written version of this course.

It’s important to remember that it’s your responsibility to close the file. In most cases, upon termination of an application or script, a file will be closed eventually. However, there is no guarantee when exactly that will happen. This can lead to unwanted behavior including resource leaks. It’s also a best practice within Python (Pythonic) to make sure that your code behaves in a way that is well defined and reduces any unwanted behavior.

realpython.com/read-write-files-python/#opening-and-closing-a-file-in-python

Hello, when I run the script to read the test file, I get the output [‘Hello World!\n] instead of the literal output from the file. What can cause this?

Sorry, I overlooked the last part that is indeed the expected output. I still wonder however how to get the literal output, but probably I will learn that later :)

file.readlines() returns a list, so you could alter this line to

print(file.readlines()[0])

This would print out the first entry in the list, and that would give you the output I think that you’re expecting.

At 4:12 he highlighted then commented out three lines of code. How did he do that as opposed to manually moving the cursor to each line and typing “#”?

@Caylor Willis In Visual Studio Code, as well as many other code editors, you can typically hit Ctrl + / to comment out the highlighted lines at once. It actually works as a toggle switch, so doing it again will uncomment the highlighted or just the current line.

Become a Member to join the conversation.


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4