简体   繁体   English

读取多个同名但扩展名不同的文件 Python

[英]read multiple files with same name but different extension Python

i trying to read and write files from folder with same names, BUT different extensions => extract data from them => rewrite.我试图从具有相同名称的文件夹中读取和写入文件,但具有不同的扩展名 => 从中提取数据 => 重写。 Here is my code:这是我的代码:

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)

i tried to do check:我试图做检查:

    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

but without succes Names of files are sub01_T1w.json, sub01_T1w.nii.gz, sub01_T1w1.json, sub01_T1w1.nii.gz ...但没有成功文件的名称是 sub01_T1w.json, sub01_T1w.nii.gz, sub01_T1w1.json, sub01_T1w1.nii.gz ...

I'd suggest working with pathlib , it's a relatively new part of the python standard library that makes working with paths and files a bit easier.我建议使用pathlib ,它是 python 标准库的一个相对较新的部分,可以更轻松地处理路径和文件。 A solution using it could look like this:使用它的解决方案可能如下所示:

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

If you want to write to them, remember to re-open the target with open(json_path, 'w') after exiting the read-only with -block.如果你想给他们写信,记得要带重新打开目标open(json_path, 'w')退出只读后with -块。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 从 Python 中具有相同文件扩展名的多个文件的目录中读取文件 - Read File From Directory with Multiple Files of the Same File Extension in Python 输入两个名称相同但扩展名不同的文件 - Input two files with same name but different extension 如何在python中批量重命名具有相同名称和不同扩展名的多个文件 - How to bulk rename multiple files with same name and different extensions in python 如何区分两个不同文件夹中具有不同扩展名的同名文件? Python - How to differentiate files with same name with different extension from two different folders? Python 使用python将多个具有相同标题但不同csv文件名的CSV文件合并为一个文件 - merging multiple CSV files in one with same header but different csv files name with python 按递增顺序重命名多个不同扩展名的同名文件 - Rename multiple file of different extension with the same name in the incremental order 如何在python中导入具有相同名称的多个文件 - How to import multiple files with same name in python 如何在Python中使用不同的名称创建多个文件 - How to create multiple files with different name in Python 在python中,如何在目录中输出具有相同扩展名的所有文件名? - In python,how to output all the files name with the same extension in a directory? 使用 Python 将具有相同文件名模式(但实际文件名不同)的多个 TSV 文件转换为 CSV - Converting multiple TSV files with same file name pattern (but different actual file names) into CSVs using Python
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM