python - 文件描述符位置

标签 python file file-descriptor

我正在尝试使用 tell() 来显示我在文件描述符上的位置,但似乎总是指向错误的位置。

在我用来读取 data.txt 文件(在代码下方)的下面的代码中,在所有运行过程中,POS 被打印到错误的位置(例如 -10、9、... ),最后打印的位置是93,和实际完全不同。

然后在接近尾声时,我打印了接下来的 5 个字节,它显示了 SPACE 之后的行。好的,这是正确的,因为我已经阅读了该行(事实上我还尝试在 readline() 之前获取 pos,然后是 fd.seek它。

但令我惊讶的是,如果我 pos=fd.tell() 然后 fd.seek(pos),它打印的正是我想要的(但没想到).

这是怎么回事?

#!/usr/bin/env python

class space:
    def read(self, filename):
        with open( filename, "r") as fd:
            hdr={}
            while True:
                line = fd.readline().split()
                if line[0] == "SPACE":
                    break
                key=line[0]
                for value in line[1:]:
                    hdr.setdefault(key, []).append(value)

                pos = fd.tell()
                print pos,line

            # Building the header
            self.header = {
                    'x0'          : int(hdr['XAXIS'][0]),
                    'dx'          : int(hdr['XAXIS'][1]),
                    'xmax'        : int(hdr['XAXIS'][2]),
                    'y0'          : int(hdr['YAXIS'][0]),
                    'dy'          : int(hdr['YAXIS'][1]),
                    'ymax'        : int(hdr['YAXIS'][2]),
                    'nobjects'    : int(hdr['NOBJECTS'][0]),
                    'objects'     : hdr['OBJECT']
                    }

            # Probably there is a best way to do this
            self.x0          = self.header['x0']
            self.y0          = self.header['y0']
            self.dx          = self.header['dx']
            self.dy          = self.header['dy']
            self.xmax        = self.header['xmax']
            self.ymax        = self.header['ymax']
            self.nobjects    = self.header['nobjects']
            self.objects     = self.header['objects']

            # Storing the POSition of File Descriptor (just for safety)
            pos = fd.tell()
            print pos

            # Why
            print fd.read(5)    # Gives me 1525

            # While
            fd.seek(pos)
            print fd.read(5)    # Gives me SPACE

            # Didn't the fd position on first fd.read(5) was not pointing to correct
            # place?

if __name__ == "__main__":
    sp=space()
    sp.read("data.txt")

运行上面的代码返回:

 %./spc.py
-10 ['XAXIS', '1525', '2', '1767']
9 ['YAXIS', '1525', '2', '2011']
21 ['NOBJECTS', '5']
35 ['OBJECT', 'YAXIS']
49 ['OBJECT', 'XAXIS']
64 ['OBJECT', 'XCOORD']
79 ['OBJECT', 'YCOORD']
93 ['OBJECT', 'DEPTH']
114
1525
SPACE

这是data.txt文件

XAXIS 1525 2 1767
YAXIS 1525 2 2011
NOBJECTS 5
OBJECT YAXIS
OBJECT XAXIS
OBJECT XCOORD
OBJECT YCOORD
OBJECT DEPTH
SPACE 29768 s1_0411
1525 1525 125000.01 125000.01 5933.09
1525 1527 125164.05 125000.01 5870.35
1525 1529 125328.09 125000.01 5836.18
1525 1531 125492.13 125000.01 5805.22
1525 1533 125656.17 125000.01 5735.52
1525 1535 125820.21 125000.01 5670.15
1525 1537 125984.26 125000.01 5617.8
1525 1539 126148.30 125000.01 5574
1525 1541 126312.34 125000.01 5538
1525 1543 126476.38 125000.01 5526
1525 1545 126640.42 125000.01 5553
1525 1547 126804.47 125000.01 5574
1525 1549 126968.51 125000.01 5588.17
1525 1551 127132.55 125000.01 5559.29
1525 1553 127296.59 125000.01 5454.46
1525 1555 127460.63 125000.01 5404.4
1525 1557 127624.68 125000.01 5356.67
1525 1559 127788.72 125000.01 5337
1525 1561 127952.76 125000.01 5323.71
1525 1563 128116.80 125000.01 5338.36

也欢迎任何其他有助于改进代码、提示和技巧的帮助。

最佳答案

我无法通过复制和粘贴数据文件和代码来重现您的问题。当我运行你的代码时(Ubuntu 14.04 上的 Python 2.7.6),输出是:

18 ['XAXIS', '1525', '2', '1767']
36 ['YAXIS', '1525', '2', '2011']
47 ['NOBJECTS', '5']
60 ['OBJECT', 'YAXIS']
73 ['OBJECT', 'XAXIS']
87 ['OBJECT', 'XCOORD']
101 ['OBJECT', 'YCOORD']
114 ['OBJECT', 'DEPTH']
134
1525 
1525 

我想,这正是您想要的。

顶部的 shebang 让我觉得你在 UNIX 机器上,我也是。因为你的 pos 只有 114,而不是 134,我猜 data .txt 是使用 Windows 创建的 line endings ,这可能是你的问题。 也许尝试使用 'rb' 而不是 'r' 打开文件或替换行尾。

documentation for file.tell 在 Windows 上打开具有 UNIX 样式行结尾的文件时提到了一个问题,反之亦然。

关于python - 文件描述符位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37338730/

相关文章:

python - 比较两条信息以找出相似之处

c# - PHP的realpath相当于C#

C套接字从接受返回的文件描述符中获取IP地址

java - Writer 不写入文件,只创建文件

python - 从文件中读取行的生成器

c - 查找最大管道数的函数

c - 为什么要在调用 fork() 之后和调用 exec...() 之前关闭所有文件描述符?我该怎么做?

python - 如何将未连接的networkx图分成多个相互不相交的连接图?

python - 基于用户参数的输出格式

python - DataFrame 应用并返回可变多行?