Zip directory with subdirectories in Robot Framework

When working with the Robot Framework, I try to pin a directory with one file and three subdirectories containing the files. I am using ArchiveLibrary and the Create Zip From Files In Directory keyword. The result is a ZIP directory containing one file in the top directory and three empty subfolders.

How to configure a library to include the contents of a subfolder?

Here's how the keyword was originally defined:

def create_zip_from_files_in_directory(self, directory, filename): ''' Take all files in a directory and create a zip package from them `directory` Path to the directory that holds our files `filename` Path to our destination ZIP package. ''' if not directory.endswith("/"): directory = directory + "/" zip = zipfile.ZipFile(filename, "w") files = os.listdir(directory) for name in files: zip.write(directory + name, arcname=name) zip.close() 

Link to the full library.

I experimented with os.walk without success.

How is the keyword used in the .robot file:

 Zip xml file ${zipfilename}= set variable komplett.zip Create zip from Files in directory ../xml/komplett/ ${zipfilename} 

If that matters, I really only need to solve this particular case, not the general one, which means that I don’t mind typing in the path to each directory, and then join in somehow that I just don’t understand how do it ... Also, I use PyCharm as an editor, not RIDE.

+5
source share
1 answer

EDIT : When using the library version 0.4 and higher, you can choose whether to include subdirectories. For instance:.

 Create Zip From Files In Directory ../xml/komplett/ no_sub_folders.zip Create Zip From Files In Directory ../xml/komplett/ dir_and_sub_folders.zip sub_directories=${true} 

The keyword for creating tar is a little different - by default it includes files in subdirectories, and now you have the option not:

 Create Tar From Files In Directory ../xml/komplett/ dir_and_sub_folders.tar Create Tar From Files In Directory ../xml/komplett/ no_sub_folders.tar sub_directories=${false} 

The default values ​​of sub_directories are based on pre-existing behavior, and not on breaking existing customs in test cases.


Original answer, for versions <0.4:

If you want to fix the library, this code should do:

 zip = zipfile.ZipFile(filename, "w") for path, _, files in os.walk(directory): for name in files: file_to_archive = os.path.join(path, name) # get rid of the starting directory - so the zip structure is top-level starting from it file_name = path.replace(directory, '') file_name = os.path.join(file_name, name) zip.write(file_to_archive, arcname=file_name) # set the desired name in the archive by the arcname argument zip.close() 

Edit: saves the subdirectory structure for in - subdirectory files. The generated file is located with the top-level target directory and all its subdirectories under it (unlike an archive that saves the full path to the target directory)

arcname controls the arguments , which is the name of the file stored in the archive - and through line 7, we save the relative directory plus the file name.

Always use os.path.join , because it automatically takes care of differences in different file systems (ntfs / linux / etc).

If the final solution works for you, do not forget to offer the patch to the library owner - return it to the community :)

+3
source

Source: https://habr.com/ru/post/1265859/


All Articles