Skip to content
Advertisement

How to extract all .rar files inside a folder (gdrive) from google colab?

I have a folder consist of many .rar files. I need to extract all .rar files inside that folder. I’ve mounted my gdrive account to the colab. What I’ve done is:

data_path = "content/gdrive/My Drive/folder"
for file in os.scandir(data_path):
    !unrar x file.path "/content/drive/path/output_folder/"

but I got error:

Cannot open file.path
No such file or directory
No files to extract

I think file.path will work since when I used:

for file in os.scandir(data_path):
    print(file.path)

It prints all file paths in that folder

How to accomplish it efficiently?

Advertisement

Answer

For using variable in !unrar, you need to use this code

file_path = '/content/drive/MyDrive/Colab Notebooks/123.rar'
!unrar x '$file_path'

So, in your situation you will have next code:

data_path = "/content/gdrive/My Drive/folder"
for file in os.scandir(data_path):
    !unrar x '$file.path' "/content/drive/path/output_folder/"
User contributions licensed under: CC BY-SA
8 People found this is helpful
Advertisement