On Nov 13, 4:54 am, Alex <
aleam...@gmail.com> wrote:
> I've made a small amount of Python code to test error condition when a
> DICOM file is invalid.
> If a file cannot be parsed correctly by pydicom, I want to rename it
> heading the string "ERROR_" in front of filename.
>
> ----------------------------
> (...omissis...)
>
> DICOM_DIR="C:\\DICOM_FILES"
>
> dcm_file_list = glob.glob(DICOM_DIR + "\\1.*")
> for df in dcm_file_list:
> log("--------------------------------------------")
> log("FOUND (probably) DICOM FILE: " + df)
> try:
> plan=dicom.read_file(df)
> except:
> log("ERROR. CANNOT PARSE THIS FILE. RENAMING IT FOR YOUR
> FURTHER ANALISYS")
> (fpath,fname)=os.path.split(df)
> os.rename(df,os.path.join(fpath,"ERROR_"+fname))
> else:
> log("OK, FILE PARSED CORRECTLY")
> ---------------------------
>
> ...
> WindowsError: [Error 32] Impossible access to the file. The file Þ is
> used by another process.
>
read_file() can also take a file-like object rather than the file
name.
try adding
from dicom.filebase import DicomFile
at the top of your code,
and then:
for df in dcm_file_list:
dfile = DicomFile(df, 'rb')
plan = dicom.read_file(dfile)
then you can dfile.close() in your exception catching.
> It look like the pydicom module doesn't release the file within
> exception.
Yes, that should be fixed within pydicom. Would you mind adding that
to the issue list?
Hope that helps
Darcy