Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • Home
  • SEARCH
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 9186187
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 17, 20262026-06-17T19:27:47+00:00 2026-06-17T19:27:47+00:00

I seem to recall cases in lower level languages that opening a file more

  • 0

I seem to recall cases in lower level languages that opening a file more than once in a program could result in a shared seek pointer. By messing around in Python a bit, this doesn’t seem to be happening for me:

$ cat file.txt
first line!
second
third
fourth
and fifth
>>> f1 = open('file.txt')
>>> f2 = open('file.txt')
>>> f1.readline()
'first line!\n'
>>> f2.read()
'first line!\nsecond\nthird\nfourth\nand fifth\n'
>>> f1.readline()
'second\n'
>>> f2.read()
''
>>> f2.seek(0)
>>> f1.readline()
'third\n'

Is this behavior known to be safe? I’m having a hard time finding a source saying that it’s okay, and it would help a lot if I could depend on this.

I’m not seeing the position as an attribute of the file object, otherwise I’d have more confidence in this. I know it could be kept internally in the iterator, but idk how .tell() would get to it in that case.

>>> dir(f1)
['__class__', '__delattr__', '__doc__', '__getattribute__', '__hash__',
 '__init__', '__iter__', '__new__', '__reduce__', '__reduce_ex__', '__repr__',
 '__setattr__', '__str__', 'close', 'closed', 'encoding', 'fileno', 'flush',
 'isatty', 'mode', 'name', 'newlines', 'next', 'read', 'readinto', 'readline',
 'readlines', 'seek', 'softspace', 'tell', 'truncate', 'write', 'writelines',
 'xreadlines']

UPDATE
On page 161 of The Python Essential Reference it states

The same file can be opened more than once in the same program (or in
different programs). Each instance of the open file has its own
file pointer that can be manipulated independently.

So it seems to in fact be safe, defined behavior

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-17T19:27:48+00:00Added an answer on June 17, 2026 at 7:27 pm

    On a modern OS (post-1969 for UNIX-like OSs, or post-2000 for Windows, and probably before that but I’m counting Win2K as the first “modern” Windows), each instance of an open file (file descriptor) has its own seek pointer. There is no magic in Python’s file class that would cause instances to share state; file is a wrapper for an ordinary C file handle, which itself encapsulates an OS file descriptor, and the implementation of file.tell() and file.seek() call the corresponding C stdio functions. (For the messy details see CPython’s fileobject.c.) There can be differences between the C library behavior and the underlying OS’s behavior, but in this particular case that’s not a factor.

    If you’re using IronPython or Jython, it’s going to use the standard .Net or Java file object for its underlying implementation, which in turn is going to use the standard C library or OS implementation.

    So your approach is fine unless you are somehow running Python on some non-standard OS with bizarre I/O behavior.

    You may get unexpected results when writing if you don’t flush in a timely manner; data can hang out in memory for some time before it actually hits the disk and is available to the other file descriptors you’ve opened on the same file. As abarnert points out in a comment, that’s problematic anyway, except in very simple cases.

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I seem to recall hearing at some point (I believe it was MIX09) that
I could very will be imagining things, but I seem to recall in Java
My memory is fuzzy but I seem to recall that Windows Vista introduced an
I seem to recall that after the publicized privacy concern with Facebook Apps and
I seem to recall that there was a plugin or Rake snippet that would
I seem to recall that there is a Firefox or Chrome plugin that allows
I seem to recall an online script that refactors JavaScript for the purpose of
I seem to recall that it is not safe to trust the value of
I seem to recall reading somewhere that the cost of a virtual call in
I seem to recall that there is an HTML tag that escapes absolutely everything

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.