python中的文件指针 [英] File pointer in python

查看:245
本文介绍了python中的文件指针的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我在使用Python处理文件时有很多问题.请帮我整理一下.

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是文件对象.

  1. 我可以假定它是文件指针吗?

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

如果是,f指向什么?是为文件结构中的第一个字节保留的空白空间?

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

我可以假定文件结构的索引为零吗?

Can I assume file structure to be zero indexed?

在微处理器中,我了解到的是指针始终指向下一条指令.在python中如何?如果我在文件中写了一个说"b"的字符,我的文件指针会指向字符"b"还是"b"旁边的位置?

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'?

推荐答案

您没有指定版本,并且文件对象在Python 2和Python 3之间的行为略有不同.具体细节有所不同.以下内容假设您使用的是Python 3,或者您使用的是Python 2.6或2.7中io模块的open版本,而不是Python 2的内置open.

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.

它不是文件 pointer ,尽管很有可能是根据幕后实现的.与C不同,Python不公开指针的概念.

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.

但是,您似乎在想的是流位置",类似于指针.这是tell()报告的数字,可以将其输入seek().对于二进制文件,它是文件开头的字节偏移量.在文本文件中,它只是对文件对象有意义的偏移量"-文档将其称为不透明数字"(即,对于文件的方式,它没有定义的 physical 含义)存储在磁盘上).但是在两种情况下,它都是从起点偏移的,因此起点为零.这仅在文件支持随机访问时才是正确的-您通常会使用随机访问权,但是准备好最终遇到您不希望使用的情况-在这种情况下,是seektell引发错误.

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.

就像处理器中的指令指针一样,流的位置是 next 操作的起始位置,而不是当前操作的结束位置.因此,是的,在将字符串写入文件后,当前位置通常将是该位置之后的一个偏移值.

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.

刚打开文件时,偏移量通常为零或文件末尾(比未获得EOF时可以读取的最大值高一个).如果您以'r'模式打开它,则为零;如果以'a'模式打开它,则为零;这对于'w'和'w +'模式是等效的,因为它们将文件截断为零.字节.

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.

这篇关于python中的文件指针的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆