Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
zipfile module error
#1
hi. first of all I have to tell you that I'm a noob.
I'm trying to extract a zip file that gets downloaded and I get error
I double checked but I can't find the mistake

PS: how do I check if ffmpeg already exists and only run it after the first time when it's been updated. should use a checksum check?

import os
import requests
import zipfile
import io
import subprocess

# Function to install the 'requests' module
def install_requests():
    try:
        # Check if 'requests' is already installed
        import requests
        print("requests module is already installed.")
    except ImportError:
        # 'requests' is not installed, so install it
        subprocess.check_call(["pip", "install", "requests"])
        print("requests module installed successfully.")


# Function to download the latest version of FFmpeg
def download_ffmpeg():
    # Fetch the latest FFmpeg release URL
    url = "https://github.com/BtbN/FFmpeg-Builds/releases/latest"
    response = requests.get(url)
    response.raise_for_status()
    
    # Extract the download URL from the response
    download_url = response.url + "/ffmpeg-*-essentials_build.zip"
    
    # Download the FFmpeg archive
    archive_response = requests.get(download_url)
    archive_response.raise_for_status()
    
    # Extract the downloaded archive
    archive = zipfile.ZipFile(io.BytesIO(archive_response.content))
    archive.extractall()
    
    # Find the extracted FFmpeg folder
    ffmpeg_folder = [folder for folder in archive.namelist() if folder.startswith("ffmpeg-")][0]
    
    # Return the path to the FFmpeg folder
    return os.path.abspath(ffmpeg_folder)


# Function to add FFmpeg to the PATH environment variable
def add_ffmpeg_to_path(ffmpeg_path):
    # Get the current PATH environment variable value
    current_path = os.environ.get("PATH", "")
    
    # Add the FFmpeg path to the current PATH
    updated_path = current_path + os.pathsep + ffmpeg_path
    
    # Set the updated PATH environment variable for the current session
    os.environ["PATH"] = updated_path
    
    # Set the updated PATH environment variable permanently
    subprocess.call(["setx", "PATH", updated_path])

# Install 'requests' module
install_requests()

# Download FFmpeg
ffmpeg_path = download_ffmpeg()

# Add FFmpeg to the PATH
add_ffmpeg_to_path(ffmpeg_path)

# Print the path to FFmpeg
print("FFmpeg path:", ffmpeg_path)
Reply
#2
(Jun-30-2023, 09:47 AM)pseudo Wrote: I get error
Please show the full error message.
Reply
#3
(Jun-30-2023, 09:56 AM)ibreeden Wrote:
(Jun-30-2023, 09:47 AM)pseudo Wrote: I get error
Please show the full error message.

I GOT IT! it's not downloading the file for some reason and I didn't add a exception so I didn't know. but I still don't know what's happening and why its not downloading. I'll go double -check the links

this is the output:
Quote:PS D:\trainee> python .\ffmpeg.py
requests module is already installed.
Traceback (most recent call last):
File "D:\trainee\ffmpeg.py", line 62, in <module>
ffmpeg_path = download_ffmpeg()
^^^^^^^^^^^^^^^^^
File "D:\trainee\ffmpeg.py", line 34, in download_ffmpeg
archive = zipfile.ZipFile(io.BytesIO(archive_response.content))
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
File "C:\Program Files\Python311\Lib\zipfile.py", line 1301, in __init__
self._RealGetContents()
File "C:\Program Files\Python311\Lib\zipfile.py", line 1368, in _RealGetContents
raise BadZipFile("File is not a zip file")
zipfile.BadZipFile: File is not a zip file

edit: those ^^^ signs were not at the beginning of the line
they were under ffmpeg_download function and the second one was after the assignment operator
Reply
#4
(Jun-30-2023, 09:47 AM)pseudo Wrote: archive_response.content
The message is clear, the archive_response.content does not contain a zip file. By the way, I visited the requested url https://github.com/BtbN/FFmpeg-Builds/releases/latest and there is no filename containing essentials_build in the page. I guess the algorithm of download_ffmpeg() needs an update.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Using zipfile module - finding folders not files darter1010 2 283 Apr-06-2024, 07:22 AM
Last Post: Pedroski55
  [Solved by deanhystad] Create a zip file using zipfile library DZ_Galaxy 2 1,178 Aug-17-2022, 04:57 PM
Last Post: DZ_Galaxy
  Can ZipFile be used to extract hidden files? AiedailEclipsed 0 1,639 Mar-22-2022, 05:21 PM
Last Post: AiedailEclipsed
  Created zipfile without all the subfolder? korenron 3 3,789 Jun-23-2021, 12:44 PM
Last Post: korenron
  Create ZIP file not working using ZipFile? korenron 1 2,122 Jun-13-2021, 04:15 PM
Last Post: korenron
  zipfile sagpal 2 2,237 Jun-03-2020, 08:35 PM
Last Post: sagpal
  vlc module error pythonprogrammer 1 2,880 Apr-23-2020, 04:16 AM
Last Post: Larz60+
  zip file variable prints <zipfile.ZipFile object at 0x7f83fd13bd90> ? Rsh 9 4,185 Jun-27-2019, 02:00 PM
Last Post: Rsh
  python zipfile.ZipFile makes zip file but can't add folders to them CashMunny 3 3,655 May-09-2018, 10:28 PM
Last Post: CashMunny
  pyhton error there is no PIL module error help! pokeboss235 1 2,923 Feb-22-2018, 03:03 AM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020