Python Files - scope of 'with'

Hi

Is it correct to say that scope of ‘with’ in python files is until file is read once, or written to once, or appended once, or the passed parameter is fulfilled once?

e.g

with open(‘how_many_lines.txt’) as lines_doc:
for each_line in lines_doc.readlines():
print(each_line)

after these lines of code, the following code does not work

each_line = lines_doc.read()
print(each_line)

this does not work, i.e gives no output, but if i call ‘with’ again then it works.

So how to know ‘with’ scope for write and append instances??

Thank you

The exercise i was trying was Iterating Through Lines

When you ask a question, don’t forget to include a link to the exercise or project you’re dealing with!

If you want to have the best chances of getting a useful answer quickly, make sure you follow our guidelines about how to ask a good question. That way you’ll be helping everyone – helping people to answer your question and helping others who are stuck to find the question and answer! :slight_smile:

A with statement does not create a name scope. It ends where it ends, it does not end somewhere in the middle of itself.

Where does a loop end?

2 Likes

The loop ends after reading the whole file.
What do u mean by ’ does not create a name scope’?

Functions have local scope. With-statements are not that, they do not have local variables.

A loop’s body is all the code that is indented under it
A with’s body is all the code that is indented under it
A function’s body is all the code that is indented under it
A <insert whatever> body is all the code that is indented under it

2 Likes

So why didn’t my second print command work?

You haven’t posted any valid python code so I’m not sure what you’re asking about

If you’re opening a file then the file is also now part of your program, it affects what the program does, so that too is something I wouldn’t know about.

2 Likes

Hi

I posted the link to the exercise… in the first post

https://www.codecademy.com/courses/learn-python-3/lessons/learn-python-files/exercises/iterating-through-lines?action=resume_content_item

1 Like

Right but if I do the exercise then I would have a piece of working code, so that wouldn’t be the code you’re asking about

You’d need to supply something that can be copied and run to obtain the same result…right?

2 Likes

let me edit my code… and post it again where i encountered that problem

plz try this

https://www.codecademy.com/courses/learn-python-3/lessons/learn-python-files/exercises/iterating-through-lines?action=resume_content_item

Don’t know what you want me to do with that.

That’s a link to a codecademy exercise. I could go there and complete the exercise. Then what, what would I be looking for?

2 Likes

Here’s an example of something that can be reproduced.

Here’s script.py:

with open('how_many_lines.txt') as lines_doc:
  for each_line in lines_doc.readlines():
    print(each_line)

And here’s how_many_lines.txt:

1. How many lines do we write on the daily,
2. Many money, we write many many many
3. How many lines do you write on the daily,
4. Say you say many money, you write many many many

Which produces this output:

1. How many lines do we write on the daily,

2. Many money, we write many many many

3. How many lines do you write on the daily,

4. Say you say many money, you write many many many

But I have no idea how that relates to what you might be doing.
There’s some use of print in the python code, and something is being printed because there is output, so I’d like to say that print worked… Right? Because “work” means “did something” and it did… something. It’s really unspecific but there was indeed some effect.

2 Likes

Maybe you mean that you read the file to end, and then kept reading. Well, what else is there after having read it all? That’s unrelated to a with-statement, removing the with statement would have the very same effect.

Or, if you did it outside the with-statement, then you would be trying to read from a closed file, which probably shouldn’t do a whole lot of useful things. Because, that’s what a with-statement does, isn’t it, closing the thing after finishing the statement.

^ but this is after having completed the exercise myself, then having started guessing about what you’re talking about, and coming up with two different guesses, and then explaining both of them, and still not knowing whether it’s what you meant at all.

2 Likes
 with open('how_many_lines.txt') as lines_doc:
>   for each_line in lines_doc.readlines():
>     print(each_line)
>   each_line = lines_doc.read()
>   print(each_line)

The question is simple.

I wrote two instances of print under the same ‘with’. The first instance gave the output and the second didn’t. I did that just to know if readlines() and read() can be used one after the another.

the read() instance didn’t give any output, says to me that with has closed the file after the file was read once. ( even though there was a call for it to read it again)

This was not done to finish reading the file but to get better understanding of ‘with’.

When i posted the link, i though you could see the code I typed???

1 Like

that’s what separate user accounts means.

with closes the file when you exit with. not in the middle. same as how loops don’t exit in the middle of their bodies

the readlines method could very well close the file after having read everything for obvious reasons. does it? don’t know, but you can read its documentation and you can ask the file object whether it’s closed. either way, there’s nothing more to read.

read_everything()
read_some_more()  # not sure what you expect
1 Like

thats exactly what i have been asking…

read_question()
write_apt_replies() #not expecting anything now

maybe not so apt, but a whole lot to pick apart and some to discard before there’s something that can be correctly answered. and I will.

Right, the title has “scope” which isn’t happening, and “with”, which isn’t causing it. I have to grab a scythe and cut some of that down.

1 Like