I have written a code in python to convert dicom (.dcm) data into a csv file. However, if I run the code for more than once on my database directory, the data is automatically getting lost/deleted. I tried searching in 'recycle bin' but could not find the deleted data. I am not aware of the process of what went wrong with the data.
Is there anything wrong with my code? Any suggestions are highly appreciated.
here is my code:
import xlsxwriter
import os.path
import sys
import dicom
import xlrd
import csv
root = input("Enter Directory Name: ")
#path = os.path.join(root, "targetdirectory")
i=1
for path, subdirs, files in os.walk(root):
for name in files:
os.rename(os.path.join(path, name), os.path.join(path,'MR000'+ str(i)+'.dcm'))
i=i+1
dcm_files = []
for path, dirs, files in os.walk(root):
for names in files:
if names.endswith(".dcm"):
dcm_files.append(os.path.join(path, names))
print (dcm_files)
with open('junk/test_0.csv', 'w', newline='') as csvfile:
spamwriter = csv.writer(csvfile, delimiter=',',
quotechar='|',
quoting=csv.QUOTE_MINIMAL)
spamwriter.writerow(["Folder Name","File Name", "PatientName",
"PatientID", "PatientBirthDate","SliceThickness","Rows"])
for dcm_file in dcm_files:
ds = dicom.read_file(dcm_file)
fileName = dcm_file.split("/")
spamwriter.writerow([fileName[1],fileName[2],
ds.get("PatientName", "None"),
ds.get("PatientID", "None"),
ds.get("PatientBirthDate", "None"),
ds.get("SliceThickness", "None"),
ds.get("Rows", "None")])