Search code examples
pythonfileuser-accounts

Python win32file remove file data


I have a data file, which is used to store details about users. I need to be able to look up a user in the file using win32file and remove the data from the file.

I have the following structure size for each user account

account = struct.pack("=250s250s", username, password)

I know how to update individual accounts using WriteFile() and SetFilePointer(), but I cannot figure out how to remove an account.


Solution

  • Seems like you're using a fixed size of 500 bytes for each entries, that means deleting an entry means deleting from the middle of the file. To do so, could either rewrite the whole file, or move each entry after the one you need to delete up 500 bytes and truncate the file.

    Also, python's built in io api should be more then capable to do this, there's no real reason to resort to the win32file api.