Bad magic number in python

Saved searches

Use saved searches to filter your results more quickly

You signed in with another tab or window. Reload to refresh your session. You signed out in another tab or window. Reload to refresh your session. You switched accounts on another tab or window. Reload to refresh your session.

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

RuntimeError: Bad magic number in .pyc file #20

RuntimeError: Bad magic number in .pyc file #20

Comments

Hi i am using Windows 10 64bit but when i run the .pyc file it comes up with this error all the time but i had to record it and slow it down to 0.05x speed so i could see it but that only just managed to show it so i screenshotted it. It is below. Thanks!

Читайте также:  Credentials json google api python

The text was updated successfully, but these errors were encountered:

Usually this is caused by wrong python version

Go into the pyc file and see what the magic number is

Then compare it to this list http://stackoverflow.com/a/514395/7430976
To see if you have the correct version.

Also what python is it you used?

Ok, so I used HexEdit to read the __init__.pyc file located in
. \ChosunTruck\ChosunTruck\linux\tensorbox\utils among other locations.

The first two byes were «03 F3» which when reversed to «F3 03» give the decimal value 62211

According to the table, this means that it was built with Python 2.5b3

@chi3236 do we need to add «Python 2.5b3» (Python 2.7) to the prerequisites?

Hmm it is PyScaffold that one so i don’t know :/

If you want we can do teamviewer and you could help me?
Another thing is, it is caused by me using windows not linux?

Python 2.7 is backwards compatible with 2.5. See if that works.

So the windows version doesn’t use tensorbox, so there is no python code to execute. It is c++, you will need Visual Studio 2013

oh i have Visual Studio 2015

That works, but you will need to build the OpenCV CUDA libraries with CMake. The current versions of OpenCV use ‘shared libraries’, meaning they took a whole bunch of libraries and put them into one. For our code, we need each library separate, also called ‘static libraries’.

Since that is pretty difficult, it is much easier to use Visual Studio 2013 and follow the troubleshooting instructions in our README to get those libraries. You can have both 2013 and 2015 on your system, it will let you choose which one you want to use when starting the program.

I cannot get Visual Studio 2013 because i have to be this subscriber

You need to login with your Microsoft account. Visual Studio 2013 is free to use once you login.

Источник

How to fix bad magic number error with zipfile module in python?

The «Bad magic number» error is a common issue encountered when working with the ZipFile module in Python. This error occurs when the code tries to extract or read a Zip file that has an invalid signature or header information. In other words, the magic number of the Zip file does not match the expected value, hence the error message. To fix this error, there are several methods that can be applied.

Method 1: Verify the Zip File

If you are facing a «Bad magic number» error while using the ZipFile module in Python, it means that the file you are trying to open is not a valid ZIP file. There are several ways to fix this error, but one of the most effective methods is to verify the ZIP file before opening it. Here’s how you can do it:

Step 1: Import the ZipFile Module

First, you need to import the ZipFile module in your Python script. You can do this by adding the following line of code at the beginning of your script:

Step 2: Verify the Zip File

To verify the Zip file, you can use the ZipFile.testzip() method. This method checks the integrity of the ZIP file by verifying the CRC and file headers of each file in the archive. If the ZIP file is corrupted, this method will raise an exception.

Here’s an example code that demonstrates how to use the ZipFile.testzip() method:

import zipfile zip_file = zipfile.ZipFile('example.zip') if zip_file.testzip() is not None: # The ZIP file is corrupted print('Bad Zip file') else: # The ZIP file is valid print('Good Zip file')

In this example, we first open the ZIP file using the ZipFile() constructor. Then, we call the ZipFile.testzip() method to verify the ZIP file. If the method returns None , it means that the ZIP file is valid. If it returns a file name, it means that the ZIP file is corrupted.

Step 3: Fix the Zip File

If the ZIP file is corrupted, you can try to fix it using the ZipFile.extractall() method. This method extracts all the files from the ZIP file to a specified directory. By doing so, it can fix any corrupted files in the archive.

Here’s an example code that demonstrates how to use the ZipFile.extractall() method to fix a corrupted ZIP file:

import zipfile zip_file = zipfile.ZipFile('example.zip') if zip_file.testzip() is not None: # The ZIP file is corrupted print('Bad Zip file') # Fix the ZIP file zip_file.extractall('fixed_zip') # Reopen the fixed ZIP file zip_file = zipfile.ZipFile('fixed_zip/example.zip') # Verify the fixed ZIP file if zip_file.testzip() is not None: print('Failed to fix the Zip file') else: print('Zip file fixed') else: # The ZIP file is valid print('Good Zip file')

In this example, we first open the ZIP file using the ZipFile() constructor. Then, we call the ZipFile.testzip() method to verify the ZIP file. If the method returns a file name, we assume that the ZIP file is corrupted. We then use the ZipFile.extractall() method to extract all the files from the ZIP file to a new directory called fixed_zip . This will fix any corrupted files in the archive. We then reopen the fixed ZIP file and call the ZipFile.testzip() method again to verify that the ZIP file is now valid.

That’s it! By following these steps, you should be able to fix the «Bad magic number» error with the ZipFile module in Python.

Method 2: Use try-except Block

To fix the «Bad magic number» error with the ZipFile module in Python, you can use a try-except block to catch the error and handle it gracefully. Here’s an example code snippet:

import zipfile try: with zipfile.ZipFile("example.zip", "r") as zip_ref: zip_ref.extractall("extracted") except zipfile.BadZipFile as e: print("Error: Bad magic number")

In this example, we try to open and extract the contents of a ZIP file using the ZipFile module. If the file is not a valid ZIP file and raises a BadZipFile exception, we catch the exception and print an error message.

It’s important to note that the BadZipFile exception is specific to the ZipFile module and indicates that the file is not a valid ZIP archive. Other exceptions may be raised for other reasons, such as file not found or permission errors.

By using a try-except block, we can handle errors gracefully and provide helpful feedback to the user.

Method 3: Check for Byte Order Mark (BOM) in Zip File

If you encounter the «Bad magic number» error when using the ZipFile module in Python, it means that the file you are trying to open is not a valid ZIP file. One possible cause of this error is the presence of a Byte Order Mark (BOM) in the file.

To check for a BOM in a ZIP file, you can use the following code:

import zipfile def has_bom(filename): with open(filename, 'rb') as f: return f.read(4) == b'\xef\xbb\xbfPK' def open_zipfile(filename): if has_bom(filename): with open(filename, 'r', encoding='utf-8-sig') as f: return zipfile.ZipFile(f) else: return zipfile.ZipFile(filename)

The has_bom function checks if the first four bytes of the file are the UTF-8 BOM followed by the ZIP file signature. If this is the case, it means that the file has a BOM and we need to open it with the utf-8-sig encoding to strip the BOM.

The open_zipfile function uses the has_bom function to check if the file has a BOM and opens it accordingly. If the file has a BOM, it is opened with the utf-8-sig encoding, otherwise it is opened normally.

You can use the open_zipfile function to open your ZIP file without getting the «Bad magic number» error:

zipfile = open_zipfile('my_zip_file.zip')

This code will open the ZIP file «my_zip_file.zip» and return a ZipFile object that you can use to extract its contents.

In summary, to fix the «Bad magic number» error with the ZipFile module in Python, you can check for a BOM in the ZIP file and open it with the utf-8-sig encoding if necessary. The code examples provided above demonstrate how to do this step-by-step.

Method 4: Use ZipFile.open() instead of ZipFile()

To fix the «Bad magic number» error with ZipFile module in Python, you can use the ZipFile.open() method instead of ZipFile() . Here is an example code:

import zipfile with zipfile.ZipFile('file.zip') as zip_file: with zip_file.open('file.txt') as file: print(file.read().decode())

In this code, we first import the zipfile module. Then we use the with statement to open the zip file and the file inside it. We use the open() method to open the file inside the zip file. We pass the name of the file as an argument to the open() method. Then we use the read() method to read the contents of the file and the decode() method to convert the bytes to a string.

You can also use the ZipFile.extract() method to extract the file from the zip file and then open it. Here is an example code:

import zipfile with zipfile.ZipFile('file.zip') as zip_file: zip_file.extract('file.txt') with open('file.txt') as file: print(file.read())

In this code, we first import the zipfile module. Then we use the with statement to open the zip file. We use the extract() method to extract the file from the zip file. We pass the name of the file as an argument to the extract() method. Then we use the open() method to open the extracted file. We pass the name of the file as an argument to the open() method. Then we use the read() method to read the contents of the file.

These are the steps to fix the «Bad magic number» error with ZipFile module in Python using the ZipFile.open() method.

Источник

Оцените статью