Click here to Skip to main content
15,919,931 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am writing a script that allows users to use a USB stick as a 2FA Login Key on their computer, but if they accidentally delete the key on the USB their computer would potentially permanently log them out, so I would like to make the key file undeletable by the user.

Can I make the drive read-only in python, or are there any other solutions to stop the file being deleted?

What I have tried:

I cannot find any documentation on doing this, except an old python 2.0 stack overflow thread, so i don't know if it is possible in python 3.11 anymore.
Updated 29-Mar-23 6:22am
Richard MacCutchan 29-Mar-23 4:10am    
If you have found a suggested sample then try it and see what happens. If it does not work then you can post the details above and people will try to help you.
Member 15627495 29-Mar-23 5:05am    
the file, or the usb key, have to be 'read only'.

by windows resource it belong to the files attributes,
for the usb key 'diskpart' can protect an usb key, or while processing 'format' action.
bizbazboz 29-Mar-23 6:00am    
can I make it read-only using python though?
do i need to get diskpart from pip, or is it built in?
Member 15627495 29-Mar-23 6:20am    
the easy way is to set the file as 'read-only',
you need to include in your script few Windows os package to interact with the Files resources and make the file key as read-only.
Dave Kreskowiak 29-Mar-23 12:23pm    
That won't work. There is nothing stopping a reasonably knowledgeable user from removing the "readonly" tag.

1 solution

The problem is your "USB Key" isn't a "key" at all. It's a drive with a file on it.

EDIT: Also, there is nothing stopping the users from copying the "key file" to multiple USB drives and handing those out, completely defeating your "security".

You can get USB Key hardware that is not a drive, completely removing the ability for users to delete the key "file".

Google for "USB hardware key".
Share this answer

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)

CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900