Is there an easy way to automate keeping track of files generated and saved in python?

132 views Asked by At

I don't know if there is an easy way of doing this that doesn't rely on manually writing down what the saved outputs from a script are so open to any suggestions. I want a function that runs at the end of my script and that automatically generates a text file with a name like: "IO_track_scriptname_date_time" Which has a list of the files I loaded and the files I saved (location links). And then saves this txt file to the desired destination.

Thank you for your help

Edit: Or any alternative way of keeping a log of inputs and outputs.

1

There are 1 answers

2
James On

Here is a thin object wrapper around the open function that tracks all of the files that are opened.

class Open:
    _open = open
    def __init__(self):
        self.opened_files = []
        self.fp = None

    def __call__(self,
                 file,
                 mode='r',
                 buffering=-1,
                 encoding=None,
                 errors=None,
                 newline=None,
                 closefd=True,
                 opener=None):

        self.fp = self._open(file, mode, buffering, encoding, errors,
            newline, closefd, opener)
        self.opened_files.append((mode, file))
        return self.fp

    def __enter__(self, *args, **kwargs):
        return self.__call__(*args, **kwargs)

    def __exit__(self, *exc_details):
        return self.fp.close()

    def __getattr__(self, attr):
        return getattr(self.fp, attr)

    def export(self, filename):
        with open(filename, 'w') as fp:
            for m, fn in self.opened_files:
                fp.write(f'({m}): {fn}\n')

To actually use it, you will need to overwrite the built-in open function with an instantiation of this class. If you have one file that you are calling, you can pop this into the __main__ block. i.e.

...

if __name__=='__main__':
   # code defining Open class here
   ...
   open = Open()
   # other code in __main__ here

   open.export("IO_track_scriptname_date_time.txt")