python - fd.seek() IOError : [Errno 22] Invalid argument

标签 python python-2.6

我的 Python 解释器 (v2.6.5) 在以下代码部分引发了上述错误:

fd = open("some_filename", "r")
fd.seek(-2, os.SEEK_END) #same happens if you exchange the second arg. w/ 2
data=fd.read(2);

最后一次调用是 fd.seek()

Traceback (most recent call last):
    File "bot.py", line 250, in <module>
        fd.seek(iterator, os.SEEK_END);
IOError: [Errno 22] Invalid argument

奇怪的是,异常只是在执行我的整个代码时发生,而不是仅在打开文件的特定部分发生。 在这部分代码的运行时,打开的文件肯定存在,磁盘未满,变量“iterator”包含正确的值,如第一个代码块。 我的错误可能是什么?

提前致谢

最佳答案

来自 lseek(2):

EINVAL

whence is not one of SEEK_SET, SEEK_CUR, SEEK_END; or the resulting file offset would be negative, or beyond the end of a seekable device.

所以仔细检查iterator的值。

关于python - fd.seek() IOError : [Errno 22] Invalid argument,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/2724015/

相关文章:

Python try...except 逗号 vs 'as' in except

python - 使用Python在文件中查找字符串并在同一行中提取整数值

Python raw_input ("") 错误

python - 如何从歌曲中寻找情绪

python - 测试 Django ModelForm 是否在自定义模型上有实例

python夏令时问题

python - 如果我使用派生类,我可以 "pickle local objects"吗?

python - 存储来自多行的唯一子字符串

Python 括号内的正则表达式

python cdist 错误 ValueError : XA must be a 2-dimensional array