J
J
Jekson2020-05-28 15:29:23
Python
Jekson, 2020-05-28 15:29:23

How to remove extra folders when creating an archive?

When creating a rar archive in Ubuntu, the archive file eventually contains several subfolders, which are formed from the file path.

What is:

folder.rar
    /home
        /y700
            /projects
                file.xlsx


What is expected
folder.rar
    file.xlsx


I create an archive like this

new_file_name = self.generate_file_name()      # Генерация имени файла
       path_to_temp_folder = os.path.dirname(BASE_DIR)
       if not os.path.exists(f'{path_to_temp_folder}/files'):
            pathlib.Path(f'{path_to_temp_folder}/files').mkdir(parents=True, exist_ok=True) # Создаю папку
        wb.save(f'{path_to_temp_folder}/files/{new_file_name}')  # Сохраняю в нее эксель файл

        archive = self.generate_zip_name(rfi) # функция генерит имя архива (string)
        to_rar = f'{path_to_temp_folder}/files' # путь к папке с файлами

        os.system("rar a {} {}".format(archive, to_rar)) # Вызов линуксовой либы для создания архива


How to get rid of extra folders?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
D
Dimonchik, 2020-05-28
@dimonchik2013

in os.system go first to a folder closer

J
Jekson, 2020-05-28
@Lepilov

Not completely but fixed, only one folder remained inside the archive
added _, to_rar = (os.path.split(to_rar))
As a result

new_file_name = self.generate_file_name()      # Генерация имени файла
path_to_temp_folder = os.path.dirname(BASE_DIR)
if not os.path.exists(f'{path_to_temp_folder}/files'):
    pathlib.Path(f'{path_to_temp_folder}/files').mkdir(parents=True, exist_ok=True) # Создаю папку
wb.save(f'{path_to_temp_folder}/files/{new_file_name}')  # Сохраняю в нее эксель файл

archive = self.generate_zip_name(rfi) # функция генерит имя архива (string)

to_rar = f'{path_to_temp_folder}/files' # путь к папке с файлами
_, to_rar = (os.path.split(to_rar))  # Новая строка
os.system("rar a {} {}".format(archive, to_rar)) # Вызов линуксовой либы для создания архива

Result
folder.rar
    folder
        file.xlsx

not quite what you need ideally but already much better

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question