Skip to content

Instantly share code, notes, and snippets.

@calini
Created August 23, 2023 10:21
Show Gist options
  • Save calini/beeddf7a425f17ff1a2e90e028ba0e9b to your computer and use it in GitHub Desktop.
Save calini/beeddf7a425f17ff1a2e90e028ba0e9b to your computer and use it in GitHub Desktop.
Convert .img files in nested directories to .iso
# made by ChatGPT, shepered by calini
#
# This script will convert all .img/.ccd/.sub files in nested directories to .iso files.
#
# Usage: python3 main.py <directory>
#
# <directory> is the directory to search for subdirectories containing .img/.sub/.ccd files.
import os
import sys
from ccd2iso import convert
def convertIn(directory):
# get all files in directory
files = os.listdir(directory)
# find any .img files
imgFiles = []
for f in files:
if f.endswith(".img"):
print("\tFound " + f + "...")
imgFiles.append(f)
# check if there are any .img files
if len(imgFiles) == 0:
print("\tNo .img files found.")
return
# check if there is only one .img file
if len(imgFiles) > 1:
print("\tMultiple .img files found: %s\n", imgFiles)
return
# convert each img file to iso using ccd2iso
for img in imgFiles:
print("\tConverting " + img + " to ISO...")
with open(directory + "/" + img, "rb") as imgFile:
# check for counterpart .iso file in the same directory
if os.path.exists(directory + "/" + img[:-4] + ".iso"):
print("\tFound " + img[:-4] + ".iso, counterpart to the .img, skipping...")
continue
with open(directory + "/" + img[:-4] + ".iso", "wb") as isoFile:
# use ccd2iso to convert .img to .iso
convert(imgFile, isoFile, progress=True, size=os.path.getsize(directory + "/" + img))
# close.img file
imgFile.close()
# close.iso file
isoFile.close()
# delete .img file
os.remove(directory + "/" + img)
print("\tDeleted " + img + "...")
# delete counterpart .ccd/.sub if exists
if os.path.exists(directory + "/" + img[:-4] + ".ccd"):
os.remove(directory + "/" + img[:-4] + ".ccd")
print("\tDeleted " + img[:-4] + ".ccd...")
if os.path.exists(directory + "/" + img[:-4] + ".sub"):
os.remove(directory + "/" + img[:-4] + ".sub")
print("\tDeleted " + img[:-4] + ".sub...")
# mock for now
print("\tDone.")
print("Starting...")
# check correct number of command line arguments
if len(sys.argv)!= 2:
print("Usage: python3 main.py <directory>")
exit(1)
# get directory from command line argument
directory = sys.argv[1]
# get all files in directory
files = os.listdir(directory)
print(f"Found {len(files)} directories\n")
# check if file is directory
for f in files:
path = os.path.join(directory, f)
if os.path.isdir(path):
print(f"Found directory: {f}:")
convertIn(path)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment