In most of the cases it raises BadRarFile but sometimes it raises RuntimeError. In short: UnRAR library raises (randomly?) different exception for the same type of error (namely, wrong password supplied). Other than using different error messages "Wrong password or defective file" and "Wrong password or something else", unfortunately, I don't see any possibility for improvement. Print("RuntimeError, possibly a wrong password") Print("Specified file doesn't seem to be a proper RAR archive") So you cannot be 100% sure if a bad password is the reason for the error. Unfortunately, your unrar library seems to raise the unspecific exception RuntimeError in case a bad password is provided. You could chain multiple except to narrow down the error. Catching RuntimeError is bad enough (yet here we can at least check the args), but if one also catches except, one cannot even differentiate between the error that (a) the password is wrong or (b) that the RAR-file is bad. How do I check if a password for a RAR-file is correct with Python's UnRAR library without chaining the exceptions? Mostly, though nothing else is wrong with the RAR-file (can be decrypted without errors by using the correct password), outputs: Rarfile.RarFile("encrypted.rar", pwd="wrong_password") The following code (which tries to “open” an encrypted RAR-file supplying a wrong password): from unrar import rarfile
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |