for header_name in glob.glob(os.path.join(folder_path, "*.json")):
for nii_name in glob.glob(os.path.join(folder_path, "*.nii")):
with open(header_name, "r") as f:
nii_meta = json.load(f)
add_stc = slicetime(header_name).tolist()
nii_meta["SliceTiming"] = add_stc
with open(header_name, 'w') as f:
json.dump(nii_meta, f, indent=2)
我试图进行检查:
h_name = os.path.splitext(os.path.basename(header_name))[0]
n_name = os.path.splitext(os.path.basename(nii_name))[0]
if h_name == n_name:
do smth with data form files
但没有成功 文件名是sub01_T1w.json,sub01_T1w.nii.gz,sub01_T1w1.json,sub01_T1w1.nii.gz ...
答案 0 :(得分:1)
我建议您使用pathlib,它是python标准库的一个相对较新的部分,它使处理路径和文件变得更加容易。使用它的解决方案如下所示:
org.springframework.integration
如果要写信给他们,请记住在退出只读from pathlib import Path
p = Path('parent/folder/of/my/files')
json_names = {f.stem for f in p.iterdir() if f.suffix == '.json'}
nii_names = {Path(f.stem).stem for f in p.iterdir() if f.suffixes == ['.nii', '.gz']}
for file_name in json_names & nii_names:
json_path = p / (file_name + '.json')
nii_path = p / (file_name + '.nii.gz')
with open(json_path) as json_file, open(nii_path) as nii_file:
... # do things with the files' contents
块之后,用open(json_path, 'w')
重新打开目标。