How to create a .rar file using python?
Asked Answered
S

2

6

I want to create a .rar file by passing file paths of the files to be archived.

I have tried the rarfile package. But it doesn't have a 'w' option to write to the rarfile handler.

Is there any other way?

Snooker answered 7/7, 2015 at 7:3 Comment(0)
A
8

How about lib-rar-python?

It's a Python wrapper around the command line rar program, but it may be limited to Linux only.

On other platforms you could just call the command line rar program directly using Python's subprocess library.

Accoutre answered 7/7, 2015 at 7:7 Comment(3)
It's limited to Linux only. Is there anything for mac?Snooker
Added approach for non-Linux platforms.Accoutre
Why is this limited to Linux? It seems that one just needs to install the rar command line program, and make sure to pass it's path it as the rarbin argument when creating an Archive object. This could even work on Windows, as long as you manage to install a rar executable which accepts the same set of parameters. See the code for details.Garbe
S
9

os.system('rar a <archive_file_path> <file_path_to_be_added_to_archive>')

Can also be used to achieve this.

Snooker answered 8/7, 2015 at 9:8 Comment(1)
Te ganaste el cielo, gracias!Hiawatha
A
8

How about lib-rar-python?

It's a Python wrapper around the command line rar program, but it may be limited to Linux only.

On other platforms you could just call the command line rar program directly using Python's subprocess library.

Accoutre answered 7/7, 2015 at 7:7 Comment(3)
It's limited to Linux only. Is there anything for mac?Snooker
Added approach for non-Linux platforms.Accoutre
Why is this limited to Linux? It seems that one just needs to install the rar command line program, and make sure to pass it's path it as the rarbin argument when creating an Archive object. This could even work on Windows, as long as you manage to install a rar executable which accepts the same set of parameters. See the code for details.Garbe

© 2022 - 2024 — McMap. All rights reserved.