File pointer in python

独自空忆成欢 提交于 2021-02-05 08:00:42

问题


I have a bunch of questions in file handling in Python. Please help me sort them out.

Suppose I create a file something like this.

>>>f = open("text,txt", "w+")
>>>f.tell()
>>>0

f is a file object.

  1. Can I assume it to be a file pointer?

  2. If so what is f pointing to ? The empty space reserved for first byte in file structure?

  3. Can I assume file structure to be zero indexed?

  4. In microprocessors what I learnt is that the pointer always points to the next instruction. How is it in python? If I write a character say 'b' in the file, will my file pointer points to character 'b' or to the location next to 'b'?


回答1:


You don't specify a version, and file objects behave a little bit differently between Python 2 and Python 3. The general idea is the same, but some of the specific details are different. The following assumes you're using Python 3, or that you're using the version of open from the io module in Python 2.6 or 2.7 rather than Python 2's builtin open.


It isn't a file pointer, although there's a good chance it is implemented in terms of one behind the scenes. Unlike C, Python does not expose the concept of pointers.

However, what you seem to be thinking of is the 'stream position', which is kindof similar to a pointer. This is the number reported by tell(), and which can be fed into seek(). For binary files, it is a byte offset from the start of the file. In text files, it is just 'an offset' which is meaningful to the file object - the docs call it an "opaque number" (ie, it has no defined physical meaning in terms of how the file is stored on disk). But in both cases, it is an offset from the start, and therefore the start is zero. This is only true if the file supports random access - which you usually will be, but be prepared to eventually run into a situation where you're not - in which case, seek and tell raise errors.

Like the instruction pointer in processors, the stream position is where the next operation will start from, rather than where the current one finished. So, yes, after you've written a string to the file, the current position will usually be one offset value past that.

When you've just opened a file, the offset will usually be zero or the end of the file (one higher than the maximum value you could read from without getting EOF). It will be zero if you've opened it in 'r' mode, the end if you've opened it in 'a' mode and the two are equivalent for 'w' and 'w+' modes since those truncate the file to zero bytes.




回答2:


  1. The file object is implemented using the C stadard library's stdio. So it contains a "file descriptor" (since it's based on stdio, "under the hood" it will contain a pointer to a struct FILE, which is what is commonly called a file pointer.). And you can use tell and seek. On the other hand, it is also an iterator and a context manager. So it has more funtcionality.
  2. It is not a pointer, but rather a reference. Keep in mind that in Python f is a name, that references a file object.
  3. If you are using file.seek(), it uses 0-based absolute positioning by default.
  4. You are confusing a processor register with file handling. The question makes no sense.



回答3:


  1. There's nothing special about a file object. Just think of it as an object
  2. the name f points to the file object on the heap, just like in l = [1, 2, 3] the name l points to the list object on the heap
  3. From the documentation, there is no __getitem__ member, so this is not a meaningful question


来源:https://stackoverflow.com/questions/22589888/file-pointer-in-python

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!