【发布时间】:2018-09-16 09:46:26
【问题描述】:
我目前正在攻读计算机科学的 A2,但我在处理随机访问文件时遇到了困难。
我正在尝试创建一个列表UsersArray,它存储一些记录数据类型UsersArray = [lion,soso,Sxia],并循环遍历列表并将每个记录存储在文件TEST.DAT 中的特定偏移量处,计算如下Address = hash(UsersArray[i].Password)。当我尝试执行File.seek(Address) 时会出现问题。它给了我一个错误并告诉我seek() 函数中的参数不正确,我不明白为什么会发生此错误。
import Users,pickle
File = open("TEST.DAT","rb+")
lion = Users.Users()
lion.Password = "ilovefood"
soso = Users.Users()
soso.Password = "cats123"
Sxia = Users.Users()
Sxia.Password = "luca<3"
UsersArray = [lion,soso,Sxia]
for i in range(3):
Address = hash(UsersArray[i].Password)
File.seek(Address)
pickle.dump(UsersArray[i],File)
File.close()
错误信息:
Traceback (most recent call last):
File "C:\Users\Vaio\Desktop\PythonA2\File Processing\RandomAccessWrite.py", line 17, in <module>
File.seek(Address)
OSError: [Errno 22] Invalid argument
[Finished in 0.1s with exit code 1]
[shell_cmd: python -u "C:\Users\Vaio\Desktop\PythonA2\File Processing\RandomAccessWrite.py"]
[dir: C:\Users\Vaio\Desktop\PythonA2\File Processing]
[path: C:\MinGW\bin;C:\Users\Vaio\AppData\Local\Programs\Python\Python36-32\Scripts\;C:\Users\Vaio\AppData\Local\Programs\Python\Python36-32\]
提前感谢您的帮助!
【问题讨论】:
-
请始终包含complete错误消息。并避免使用
range:for user in UserArray:总是更安全、更高效。 -
完整的错误信息是:Traceback(最近一次调用最后一次):文件“C:\Users\Vaio\Desktop\PythonA2\File Processing\RandomAccessWrite.py”,第 17 行,在
File.seek(Address) OSError: [Errno 22] Invalid argument -
请在问题中包含它,格式很好,这就是它所属的地方。
-
您的文件中是否包含以前存储的用户数据?如果不是,那么
File.seek(Address)对于0以外的任何地址都是无效的。 -
如果您在 64 位平台上,随机字符串哈希的数值几乎肯定会达到 quintillions 或更高 - 远远超过任何文件可能具有的长度。尝试打印出
Address,看看它实际上是多么可笑。
标签: python hash record binaryfiles file-processing