0

i am currently undergoing my A2 studies in Computer Science and i am having difficulties with random access file processing.

I am trying to have a list UsersArraywhich stores some record data types UsersArray = [lion,soso,Sxia] and loop through the list and store each record in the File TEST.DAT at a specific offset calculated like this Address = hash(UsersArray[i].Password). The problem occurs when i try to do File.seek(Address). It gives me an error and tells me the argument in seek() function is not correct, and i don't understand why this error occurs.

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()

Error Message:

    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\]

Thank you for the help in advance!

luca ion
  • 3
  • 3
  • 1
    Please _always_ include the _complete_ error message. And avoid using `range`: `for user in UserArray:` is always safer and more efficient. – DYZ Sep 15 '18 at 18:02
  • The full error message is: Traceback (most recent call last): File "C:\Users\Vaio\Desktop\PythonA2\File Processing\RandomAccessWrite.py", line 17, in File.seek(Address) OSError: [Errno 22] Invalid argument – luca ion Sep 15 '18 at 18:04
  • Please include it in the question, nicely formatted, that's where it belongs. – DYZ Sep 15 '18 at 18:05
  • 1
    Does your file have data about the users previously stored in it? If not, then `File.seek(Address)` is invalid for any address other than 0. – DYZ Sep 15 '18 at 18:06
  • Possible duplicate of [fd.seek() IOError: \[Errno 22\] Invalid argument](https://stackoverflow.com/questions/2724015/fd-seek-ioerror-errno-22-invalid-argument) – ivan_pozdeev Sep 15 '18 at 18:23
  • @DYZ the TEST.DAT file is empty when i try running the program. The thing is i tried the exact same code for only one record, and sometimes the code runs and enters the record in the file at the end of the file after many blank lines (depending on the hash calculated) I feel like python doesnt like having the Adress variable passed in the seek() function, because everything runs smoothly if i dont use the variable Address and i just pass a normal integer in seek(). – luca ion Sep 15 '18 at 18:23
  • 2
    If you are on a 64-bit platform, the numeric value of random string hash is almost certainly going to be in the quintillions or higher - far beyond the length that any file could possibly have. Try printing out `Address` and seeing just how ludicrous it actually is. – jasonharper Sep 15 '18 at 18:32

1 Answers1

0

I am inclined to believe that jasonharper nailed the issue. I replicated your code using my own user objects and commented out the pickle.dump() line. I was able to print both the user with the corresponding hash value without any issues. Then I uncommented pickle.dump() and used my own (small) iterative value to use in File.seek(); when I did this, everything worked fine and python wrote to the file. I think the hash values that you're calculating are too large to be written to the file. Not sure if it's part of your assignment or not, but those hash values won't work as file offsets.

GNinja490
  • 16
  • 3