#๐ what is best way to unarchive archives in python?
18 messages ยท Page 1 of 1 (latest)
@remote sundial
Remember to:
- Ask your Python question, not if you can ask or if there's an expert who can help.
- Show a code sample as text (rather than a screenshot) and the error message, if you've got one.
- Explain what you expect to happen and what actually happens.
:warning: Do not pip install anything that isn't related to your question, especially if asked to over DMs.
i can not think of a universal method for handling these different formats, but i do know two things:
- RAR is not an open format so you'll need to use a binary from RARLabs to unarchive these files (or a 3rd party library that uses such binary)
- python has the
zipfilemodule for zip archives,but afaik it does not support encrypted files (password protected)edit: it can but can not create encrypted archives
def unarchive(file_path, destination, password=None):
if password is not None:
password = password.encode() # convert password to bytes
if file_path.endswith('.zip'):
with zipfile.ZipFile(file_path, 'r') as zip_ref:
zip_ref.extractall(destination, pwd=password)
elif file_path.endswith('.rar'):
with rarfile.RarFile(file_path, 'r') as rar_ref:
rar_ref.extractall(destination, pwd=password)
elif file_path.endswith('.7z'):
with py7zr.SevenZipFile(file_path, mode='r', password=password) as z:
z.extractall(destination)
else:
print("Unsupported file format")
Hey @remote sundial!
It looks like you pasted Python code without syntax highlighting.
Please use syntax highlighting to improve the legibility of your code and make it easier for us to help you.
To do this, use the following method:
```py
print('Hello, world!')
```
This will result in the following:
print('Hello, world!')```
You can **edit your original message** to correct your code block.
if you've found a good library for each format, that is probably a fine method
does zip needs password.encode()?
because then im using password.encode(), 7z not work but .zip works
according to the python documentation, ZipFile.open requires a bytes object for the pwd argument
but you'll have to check the docs of the 7z library as well
7z works fine
not sure about rar, but i will test
i found out that in python so annoying
i though you can use 7z library and unarchive any extension archives
each archive "extension" uses a different format that needs to be supported
ye i understand, but weird that no one put all archive algos in one library
This help channel has been closed and it's no longer possible to send messages here. If your question wasn't answered, feel free to create a new post in #1035199133436354600. To maximize your chances of getting a response, check out this guide on asking good questions.
๐ what is best way to unarchive archives in python?