clalancette / pycdlib

Python library to read and write ISOs
GNU Lesser General Public License v2.1
147 stars 38 forks source link

ValueError: seek of closed file - add actual files to an image #114

Open milahu opened 1 year ago

milahu commented 1 year ago

please add example on how to add actual files to an image

a naive attempt fails with ValueError: seek of closed file

input_paths = ["test.txt"]
output_path = "test.iso"
iso = pycdlib.PyCdlib()
iso.new(udf="2.60")
for input_path in input_paths:
    size = os.path.getsize(input_path)
    iso_path = "/" + input_path
    udf_path = "/" + input_path
    with open(input_path, "rb") as file_handle:
        iso.add_fp(file_handle, size, iso_path, udf_path=udf_path)
iso.write(output_path) # ValueError: seek of closed file
iso.close()

fixed version:

 output_path = "test.iso"
 iso = pycdlib.PyCdlib()
 iso.new(udf="2.60")
+file_handles = []
 for input_path in input_paths:
     size = os.path.getsize(input_path)
     iso_path = "/" + input_path
     udf_path = "/" + input_path
-    with open(input_path, "rb") as file_handle:
-        iso.add_fp(file_handle, size, iso_path, udf_path=udf_path)
+    file_handle = open(input_path, "rb")
+    iso.add_fp(file_handle, size, iso_path, udf_path=udf_path)
+    file_handles.append(file_handle)
 iso.write(output_path)
 iso.close()
+for file_handle in file_handles:
+    file_handle.close()
input_paths = ["test.txt"]
output_path = "test.iso"
iso = pycdlib.PyCdlib()
iso.new(udf="2.60")
file_handles = []
for input_path in input_paths:
    size = os.path.getsize(input_path)
    iso_path = "/" + input_path
    udf_path = "/" + input_path
    file_handle = open(input_path, "rb")
    iso.add_fp(file_handle, size, iso_path, udf_path=udf_path)
    file_handles.append(file_handle)
iso.write(output_path)
iso.close()
for file_handle in file_handles:
    file_handle.close()