Re-reading file when it is in open mode

I am working on scrapping . i read file with open() function to scrape things. Now, since my file is open as some name and i want re read file but without stopping it .is there any method to do ?

You can go back to the beginning of the open file and read from the
beginning again. (But why bother? You have already read it.)

For this example, first I create a file:

>>> with open('myfile.txt', 'w') as f:
...     f.writelines(['Hello world!\n',
...                   'My hovercraft is full of eels.\n'])
... 
>>> 

Then I can read from that file, twice, without closing and reopening.

>>> with open('myfile.txt', 'r') as f:
...     print(f.read())
...     print('-----')
...     pos = f.seek(0)
...     print(f.read())  # Second time.
... 
Hello world!
My hovercraft is full of eels.

(@steven.daprano, missing output of two prints). :slight_smile:

Sending this message again, because the Discuss software truncates
messages when it sees a line containing multiple hyphens and nothing
else on its own.

Does anyone know where I can report bugs in Discuss?

You can go back to the beginning of the open file and read from the
beginning again. (But why bother? You have already read it.)

For this example, first I create a file:

>>> with open('myfile.txt', 'w') as f:
...     f.writelines(['Hello world!\n',
...                   'My hovercraft is full of eels.\n'])
... 
>>> 

Then I can read from that file, twice, without closing and reopening.

>>> with open('myfile.txt', 'r') as f:
...     print(f.read())
...     print('xxx-----xxx')
...     pos = f.seek(0)
...     print(f.read())  # Second time.
... 
Hello world!
My hovercraft is full of eels.
xxx-----xxx
Hello world!
My hovercraft is full of eels.

But, if the file on disk has been changed by some other process or
thread, I don’t know what will happen. You may or may not get the new
content. I expect that will depend on the OS and file system. On my
Linux system, I got the old content, not the new content.

The only way to be sure to get the new content of the file is to close
the file and reopen it.