如何通过Python脚本在Linux系统中一次性解压多卷RAR归档文件
rar x) Got it, I've been in this situation before—dealing with split RAR archives can be a pain if you don't have the right workflow down. Below is a Python script that replicates the behavior of rar x for multi-volume RARs (like Filename-001.rar, Filename-002.rar, etc.) on Linux, plus all the details you need to get it working smoothly.
Prerequisite: Install unrar
First, you need the unrar tool installed on your system—Python doesn't have a built-in RAR handler that's reliable for split archives, and using the system's unrar aligns with how rar x works natively.
For Debian/Ubuntu-based distros:
sudo apt update && sudo apt install unrar
For Fedora/RHEL-based distros:
sudo dnf install unrar
The Python Script
This script takes the first volume of your split RAR as input, checks for common issues, and calls unrar x to handle the rest (just like the rar x command would).
import os import subprocess import sys def extract_split_rar(first_rar_path): # Check if the specified file exists if not os.path.exists(first_rar_path): print(f"Error: File '{first_rar_path}' doesn't exist.") return False # Verify this is likely the first volume of the split archive valid_first_volume_suffixes = ("-001.rar", ".part1.rar") if not first_rar_path.endswith(valid_first_volume_suffixes): print("Warning: This might not be the first volume of the split RAR.") print("Make sure you're pointing to the initial volume (e.g., Filename-001.rar or Filename.part1.rar)") proceed = input("Want to try extracting anyway? (y/n): ").strip().lower() if proceed != 'y': return False # Run the unrar x command (matches rar x behavior: preserves directory structure) cmd = ["unrar", "x", first_rar_path] try: # Execute the command and capture output result = subprocess.run(cmd, check=True, stdout=subprocess.PIPE, stderr=subprocess.PIPE, text=True) print("\nExtraction finished successfully!") print("Command output:") print(result.stdout) return True except subprocess.CalledProcessError as e: print(f"\nExtraction failed: {e.stderr}") return False except FileNotFoundError: print("Error: 'unrar' isn't installed. Follow the prerequisite steps above to install it.") return False if __name__ == "__main__": # Check for correct command-line arguments if len(sys.argv) != 2: print("Usage: python3 extract_split_rar.py <path-to-first-rar-volume>") print("Example: python3 extract_split_rar.py ./Documents/MyArchive-001.rar") sys.exit(1) first_rar_file = sys.argv[1] extract_split_rar(first_rar_file)
How to Use the Script
- Save the code above as
extract_split_rar.py - Make it executable (optional but convenient):
chmod +x extract_split_rar.py - Run it with the path to your first split RAR volume:
python3 extract_split_rar.py ./MyArchive-001.rar
Key Notes
- All volumes in one directory: Ensure all split RAR files (001, 002, etc.) are in the same folder as the first volume—
unrarwill automatically detect and use them. - Preserves directory structure: Just like
rar x, the script usesunrar xwhich extracts files while keeping their original folder hierarchy. - Handles common naming schemes: Works for both
-001.rarand.part1.rarstyle split archives.
内容的提问来源于stack exchange,提问作者tlfSnow

