0

I have the code which unzip file from path. But I don't know how to write code that skip or delete folder with unzipped files when already exists. Thank you for advice.

    def unzip_file(self, path):
        # Load file - function that reads a GTFS ZIP file. 
        #path = self.dockwidget.input_dir.filePath()
        name = os.path.splitext(os.path.basename(path))[0]
        # Create a folder for files. 
        path1 = os.path.join(os.path.dirname(path), name)
        os.mkdir(path1) 
        # Extracts files to path. 
        with ZipFile(path, 'r') as zip: 
            # printing all the contents of the zip file 
            zip.printdir() 
            zip.extractall(path1) 
        # Select text files only. 
        print(path1)
        print(path)
        files = []
        # r=root, d=directories, f = files
        for r, d, f in os.walk(path1):
            for file in f:
                exten = os.path.splitext(os.path.basename(file))[1]
                if exten == '.txt':
                    files.append(os.path.join(r, file))
        return files

Progman
  • 16,827
  • 6
  • 33
  • 48

1 Answers1

0

Check the value of os.path.exists(path) and set your logic accordingly.

https://docs.python.org/3.7/library/os.path.html#os.path.exists

Alex Reynolds
  • 95,983
  • 54
  • 240
  • 345