FAQ: Learn Python: Files - What's With "with"?

This community-built FAQ covers the “What’s With “with”?” exercise from the lesson “Learn Python: Files”.

Paths and Courses
This exercise can be found in the following Codecademy content:

Learn Python 3

FAQs on the exercise What’s With “with”?

There are currently no frequently asked questions associated with this exercise – that’s where you come in! You can contribute to this section by offering your own questions, answers, or clarifications on this exercise. Ask or answer a question by clicking reply (reply) below.

If you’ve had an “aha” moment about the concepts, formatting, syntax, or anything else with this exercise, consider sharing those insights! Teaching others and answering their questions is one of the best ways to learn and stay sharp.

Join the Discussion. Help a fellow learner on their journey.

Ask or answer a question about this exercise by clicking reply (reply) below!

Agree with a comment or answer? Like (like) to up-vote the contribution!

Need broader help or resources? Head here.

Looking for motivation to keep learning? Join our wider discussions.

Learn more about how to use this guide.

Found a bug? Report it!

Have a question about your account or billing? Reach out to our customer support team!

None of the above? Find out where to ask other questions here!

Hello,

I hope all is well. I have a question regarding this exercise:

Even though we defined the ‘punchline’ inside of the “with-block,” we can still access outside of that block. This seems to go against the scope of variables and functions (assuming that the with-block is a function). Is this a correct observation? If so, what’s happening?

Thank you again.

1 Like

with is not a function. You could wrap your with block in a function, that is possible.

with just like if (and others) is a block, but python doesn’t have block scope.

Just wanted to leave this here. Nice one.

2 Likes

lmao yes, I’m not sure how many people actually went thru to see this lol

for those wondering why we can access variables setup outside the with block its because with doesnt create scope

what is the function open()?

Recommend you bookmark this page and make copious use of it in your learning:

Built-in Functions — Python 3.12.0 documentation

With regard to your question:

7. Input and Output — Python 3.12.0 documentation

This is a good question, I was also going to ask this here,

with open(‘fun_file.txt’) as close_this_file:

setup = close_this_file.readline()
punchline = close_this_file.readline()

print(setup)

In this code, even after exiting the indented ‘with’ block, I can still access the setup variable, why? can anyone explain me clearly, I am a beginner.
As I guess, so once we open any file, python saves it in memeory, so all the stuffs already gets saved, then we can access anywhere, it’s right?

@joysasha, please check out How do I format code in my posts? to see how to post codes into the forum. Python depends on proper indentation to run and having the indentation displayed helps us troubleshoot your issues.

As to your question, as mentioned by @stetim94 above python doesn’t have block scope. The only time we have encountered so far where there’s scope within and without a code block is when defining functions. With is not a function, even if it utilizes a block, simlar to how if also utilizes a block format, but is in itself not a function and doesn’t limit the access of variables created within.

Python doesn’t save a file into memory automatically when you open it. It’s saved into memory because in the code we explicitly saved it via defining the content of the file into the variables setup and punchline.

Hope this helps!

On the basis you might have added to this topic, we’re supportive. Months later is stretching the need basis. Let’s hope you are adding to a knowledge base, and not just posting to post.

1 Like

@mtf I responded based on the fact that it’s a question that’s came up and not fully answered, “relatively” new, and the user appears to be active and may still be seeking answer (seen two days ago).

We’re awaiting their reply…