I have folder with 4 rar (zipped) files.
screenshot #1: rar files and names
This is the python code to unrar them:
#!/usr/bin/python import os, zipfile, pyunpack, sys import tkinter as tk from tkinter import filedialog from tkinter.filedialog import askdirectory root = tk.Tk() root.withdraw() basis_folder = askdirectory(title='Select Folder') for root, dirs, files in os.walk(basis_folder): for filename in files: if filename.endswith(".rar") : print('RAR:'+os.path.join(root,filename)) elif filename.endswith(".zip"): print('ZIP:'+os.path.join(root,filename)) name = os.path.splitext(os.path.basename(filename))[0] if filename.endswith(".rar") or filename.endswith(".zip"): try: arch = pyunpack.Archive(os.path.join(root,filename)) # os.mkdir(name) arch.extractall(directory=root) os.remove(os.path.join(root,filename)) except Exception as e: print("ERROR: BAD ARCHIVE "+os.path.join(root,filename)) print(e) try: # os.path.join(root,filename)os.remove(filename) pass except OSError as e: # this would be "except OSError, e:" before Python 2.6 if e.errno != errno.ENOENT: # errno.ENOENT = no such file or directory raise # re-raise exception if a different error occured sys.exit() os._exit(0)
After unrar process, the code deletes the rar files and extract the files inside rar files. I want the rename of the files as file1 from rarfile1, file2 from rarfile1, file3 from rarfile1, file1 from rarfile2, so on...
This is the result I want:
[screenshot #2: unrar files and their new names]
https://stackoverflow.com/questions/65444082/unrar-the-files-with-the-name-of-the-rar-files December 25, 2020 at 07:39AM
没有评论:
发表评论