I don’t know much about Python. Prompt the correct code algorithm for the following: I am downloading a zip file. I need to extract it to the some_name folder, and then go into this folder and find the file with the .mod extension and pick its name, then rename the some_name folder to the name from .mod and pack it back into the archive. Beaver good to everyone!
Try this for renaming:
import zipfile from io import BytesIO import os ### if you have zip archive in file z=zipfile.ZipFile('your.zip') ### if you have zip archive in bytes z=zipfile.ZipFile(BytesIO(your_bytes)) ### extract to some_name z.extractall('some_name') ### iterate through file names for name in z.filelist: if name.endswith('.mod'): break ### split extension bare=os.path.splitext(name) os.rename('some_name',bare)
And this for packing into zip archive
newz=zipfile.ZipFile('new.zip','w') for q in os.path.listdir(bare): newz.write(os.path.join(bare,q)) newz.close()
You don’t really need to extract the zip file, if you want just rename your folder ,use this:
for name in z.filelist: if name.endswith('.mod'): break os.rename('some_name',name)