Handling parsing exception under Win32

15 views
Skip to first unread message

Alex

unread,
Nov 13, 2009, 4:54:02 AM11/13/09
to pydicom
Hi dear,

I start using your pydicom module under Windows XP (32bit) and Python
2.6.4 (installed today) and pydicom 0.9.3
Usually I use Linux system but for this small project I need to run it
in a Windows environment.

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")
---------------------------

and I've put in DICOM_DIR a totally wrong file: a ZIP file called
1.2.3.4.zip
Now I want to test exception but I've a problem.

When Python interpreter reach os.rename() I've the following Windows
error:
WindowsError: [Error 32] Impossible access to the file. The file Þ is
used by another process.

It look like the pydicom module doesn't release the file within
exception.
Under Unix there is not this problem because Unix doesn't matter about
open file, but Window check the file releasing.

Any suggestion ?

Darcy Mason

unread,
Nov 13, 2009, 1:14:10 PM11/13/09
to pydicom

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

Alex

unread,
Nov 15, 2009, 7:57:21 AM11/15/09
to pydicom
>
> read_file() can also take a file-like object rather than the file
> name.
> try adding
(...omissis...)

The workaround works fine :-)

Thank you very much for your kind suggestion.
Reply all
Reply to author
Forward
0 new messages