2012-10-08 73 views
2

我有一張我想分發的ISO映像。但是,爲了讓用戶更容易設置,我想爲每個.iso文件添加一個唯一的.config文件。使用Python修改.iso文件

有沒有辦法使用python來修改iso文件?

回答

3

有一些已知的瀏覽方式或解析ISO文件與Python庫(見this question),但添加文件到ISO將需要修改的文件系統 - 這絕對是不會妄自菲薄。

您可以嘗試在您的文件系統上掛載ISO,從Python進行修改,然後再次卸載它。一個非常簡單的例子,將在Ubuntu下工作:

ISO_PATH = "your_iso_path_here" 

# Mount the ISO in your OS 
os.system("mkdir /media/tmp_iso") 
os.system("mount -o rw,loop %s /media/tmp_iso" % ISO_PATH) 

# Do your Pythonic manipulation here: 
new_file = open("/media/tmp_iso/.config", 'w') 
new_file.write(data) 
new_file.close() 

# Unmount 
os.system("umount /media/tmp_iso") 
os.system("rmdir /media/tmp_iso") 

您需要使用subprocess代替os.system,除其他事項外,但這是一個開始。

+0

謝謝。有趣。所以如果我修改.iso文件,它會改變校驗和的某個位置嗎? – Alexis

+0

不確定ISO本身內部校驗和的範圍,但最終會改變它的文件系統,這是最好留給維護良好的庫的工作。你肯定會*改變整個ISO的校驗和。 –

+0

是.iso和.img相同嗎? – Alexis