对于打开的(..)行中意外挂起中间文件

当以这样的最基本的方式读取文件时:

with open('/tmp/abs/path/file.txt', 'rb') as fh: for line in fh: print('New line...' # calculations print('Last line of line-loop') print('Ended up outside') # <-- Never reaches this either, which is "fine". 

文件操作挂起中间文件,最后一个输出是“线循环的最后一行”,意思是由于某种原因for line in fh的操作挂起。

文件大小是5025728020字节
文件位置:26957152字节

所以我想我会使用epoll()来监视阻止读取操作:

 with open('/tmp/abs/path/file.txt', 'rb') as fh: watcher = select.epoll() watcher.register(fh.fileno(), select.EPOLLIN) 

但是,如果operation not permitted ,那么这个operation not permitted失败,在文件,套接字和标准输出pipe道上,这个过程已经有很多次了,但不是在这台机器上呢? 还是我只是幸运地在一些特殊的磁盘文件之前?

Debian,v7.5 Python,v3.3.5

令我困惑的是我有三个文件,一个是十亿字节,另一个只是3296字节。 较大和较小的文件与相同的代码完美地工作,所提到的不是。

这使我困惑不已,甚至不知道从哪里开始。

做了python3.3 -m trace --trace script.py ,它说:

 script.py(112): for line in fh: script.py(113): print(fh.tell()) 124124124 script.py(112): for line in fh: 

在那里,它永远.. ..

编辑:它总是挂在同一个地方,尾巴也不能超过这一点。
运行: tail -c+26956052 /file | head tail -c+26956052 /file | head
给我一条线,只有一条线。 它应该给我更多。 只运行tail /file也会挂起

试过的file /file ,它给了我:
... ASCII text, with very long lines

Martijn让我思考,突然间我记得“dd”是一件令人感叹的事情。
dd if=file ibs=1 skip=26957150 count=100给我:

 ; 100+0 records in 0+1 records out 100 bytes (100 B) copied, 5.5e-05 s, 1.8MB/s 

这个文件/硬件有一些错误。

strace cat /file > /dev/null给出:

 read(3, " some data that is correc"..., 32768) = 32768 write(3, " some data that is correc"..., 32768) = 32768 read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...", 32768) = 32768 write(1, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...", 32768) = 32768 read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...", 32768) = 32768 write(1, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...", 32768) = 32768 read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...", 32768) = 32768 write(1, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...", 32768) = 32768 read(3, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...", 1556) = 1556 write(1, "\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0\0...", 1556) = 1556 read(3, "", 32768) = 0 close(3) = 0 close(1) = 0 close(2) = 0 exit_group(0) = ? 

所有的帮助是apriciated,我会调查这个文件的文件传输。
该文件通过一个预定的10分钟的窗口通过scp在64MB / s的LAN之间传输。 有些事情可能会出错,也许rsync会做得更好。

rsync有一些优点,自动校验和提供额外的保护(只是scp)对内存中的损坏,但据我所知在写入后不重新验证目标文件。

如果在文件传输过程中出现错误,校验和将会失败,您仍然应该能够读取垃圾。

读取失败的事实表明文件系统或介质损坏。