3

I am using python to directly access the bytes written on a compact flash card. With windows (the OS I am currently using) a drive can be accessed directly by adding a '\\\\.\\' (\\.\ when printed) before the drive name. The result is a string like '\\\\.\\G:'. Opening and writing these drives work fine; But after some of my code executes o.close() and o.seek() both fail.

f=open("\\\\.\\G:",'rb+') # use 'rb+' for disk/drive/card writing
f.seek(512*3)             # multiples of 512 are required (here seek works fine)
f.write(512*b'x')         # fill sector with the letter x
...                       # some of my code
f.close()                 # Errorno 22 : invalid argument
f.seek(0)                 # Also an errorno 22, even if file is open

This suggests the problem is in my other code.... However that code only uses the file to seek and write, which using those functions there causes no errors.

Here is my exact error message:

Traceback (most recent call last):
  File "NFS.py", line 139 in <module>
    make_fs(args.input, fo)
  File "NFS.py", line 12 in make_fs
    root_data, ls=walk(i, o, tmp)
  File "NFS.py", line 34 in walk
    o.seek(512*i)
OSError: [Errorno 22] Invalid argument

Why would python (or the OS) give me 'invalid argument' when seeking to zero or closing the file? Remember, of course, that the file is really a hardware compact flash card.

Nuclear_Man_D
  • 142
  • 11
  • why not add try, catch block around them, that way yo might catch something – BlooB Oct 11 '17 at 20:50
  • @BlooB I added the traceback for your examination. No errors with regular files and it used to work fine with compact flash. – Nuclear_Man_D Oct 13 '17 at 03:57
  • 1
    Just a stab in the dark - if you are using a larger CF card than previously, might the sector sizes now be larger? If they are 1024 Bytes now, 512*3 would not match a sector boundary and would fail. – SiHa Oct 13 '17 at 07:13
  • @Nuclear_Man_D something really stupid, but can you close() the file, than open(), than try seek(), tell me what happens – BlooB Oct 13 '17 at 17:52
  • @SiHa I did not think of that - I am used to accessing the compact flash on the Z80 processor through an IDE interface. The blocks are multiples of 512 there, but maybe a new protocol changes things a bit... Thank you. I will look into that. – Nuclear_Man_D Oct 14 '17 at 19:29
  • @BlooB yea, I tried closing, but closing it creates the same error as seek. – Nuclear_Man_D Oct 14 '17 at 19:29

0 Answers0