-
Notifications
You must be signed in to change notification settings - Fork 0
/
pycomsort.py
41 lines (38 loc) · 1.27 KB
/
pycomsort.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Tue Oct 15 15:36:52 2019
@author: kayti thorn
"""
import pydicom
import os
import glob
import os.path
#directory = os.getcwd()
directory = "/Users/kayti/Desktop/pycom_sort_test/"
subjids = os.listdir(directory)
if '.DS_Store' in subjids:
subjids.remove('.DS_Store')
for ids in subjids:
path = directory + ids + "/**/*"
dcmspath = glob.glob(path, recursive=True)
dcmspath2=dcmspath[100]
dcmpath = os.path.dirname(dcmspath2)
dcms = os.listdir(dcmpath)
if '.DS_Store' in dcms:
dcms.remove('.DS_Store')
for dcm in dcms:
dcmhdr = pydicom.dcmread(dcmpath + '/' + dcm)
seq = dcmhdr.ProtocolName
if os.path.exists(directory + '/' + ids + '/' + seq):
oldfile=os.path.join(dcmpath + '/' + dcm)
newfile=os.path.join(directory + '/' + ids + '/' + seq + '/' + dcm)
os.rename(oldfile,newfile)
else:
os.mkdir(directory + '/' + ids + '/' + seq)
oldfile=os.path.join(dcmpath + '/' + dcm)
newfile=os.path.join(directory + '/' + ids + '/' + seq + '/' + dcm)
os.rename(oldfile,newfile)
for entry in os.scandir(directory + '/' + ids):
if os.path.isdir(entry.path) and not os.listdir(entry.path) :
os.rmdir(entry.path)