Ejemplo n.º 1
0
    def write_csv(self, path=None, mode='a'):
        """ Write the collected books information to a given CSV file
            Append if the file already exists

        Parameters
        ----------
        path : str (default is the category_name)
            The path including the file name (without the extension to the csv)
        mode : str (default is 'a')
            The file mode used to open the file (r,r+,w,w+,a,a+,x,x+)
        """

        if self.books == []:
            self.collect()

        if path is None:
            path = self.name.lower().replace(' ', '_')

        fields = self.books[0].get_headers()
        headers = {fields[i]: fields[i] for i in range(len(fields))}

        if not os.path.exists(f'{path}.csv') or (mode != 'a' and mode != 'a+'):
            FileIO.write(path, fields, headers, mode)

        for book in self.books:
            FileIO.write(path, fields, book.to_dict(), 'a')
Ejemplo n.º 2
0
 def test_write(self):
     filepath = 'testwrite'
     FileIO.write(filepath, ['a'], {'a': 'hello'}, 'w')
     assert path.exists(f"{filepath}.csv") is True
     remove(f"{filepath}.csv")