Tag Archives: zipfile.BadZipFile: File is not a zip file

Error when manipulating files: zipfile.BadZipFile: File is not a zip file

When writing code, the recorder pursues decoupling, and wants the program to see if the path file exists. The OS module is usually used to operate the file or path. If it does not exist, the file is created under the current path for subsequent operations. As far as I know, the OS module does not give a method to create a file, which requires us to complete the “road safety first” through another way. At that time, we used the general with open to create a file. OK, no problem. The creation was successful. The next problem is the error of zipfile.badzipfile: file is not a zip file during data storage, It’s said that the file that is not zip is rushing to work. Suddenly, it’s a flash of inspiration. When the pandas module is used, it’s just that the file can be operated. In the end, the overall code has not changed. The only change is to attach the code at the step of creating the file

def isfile(self, file):
        print(file)
        if not os.path.isfile(file):
            df = pd.DataFrame(columns=['name', 'shake number', 'weibo', 'profile', 'estimated sales']) # Create a table object, without creating the content first
            df.to_excel(self.filr, index=False) # Save the object as an .xlsx file
        return file

Call the function to execute retuan and return the path URL for subsequent operation