read multiple files with same name but different extension Python

Relyativist

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 ...

Arne

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. 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.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Read File From Directory with Multiple Files of the Same File Extension in Python

Delete files same name but different file extension

How to differentiate files with same name with different extension from two different folders? Python

zip multiple files with the same name but different extensions

VS Code: Excluding files with same name but different Extension

Rename multiple files with different names to same name and different numbers

How to create multiple files with different name in Python

How to read several resource files with the same name from different JARs?

for loop calling two different files (same name, different extension) across several files

Read Multiple raster with the same name contained in different folders

Need to copy files to existing directory and remove files already there with the same name but different extension

How can I copy multiple files in the same directory with different names but same extension in bash?

python read csv files with same basename and save as different dataframes

Using angular I want to input two files with same name but different file extension by doing the input of just one

From files with same name but different extension(*.pdf and *.rtf), if *.rtf is newer, output message on console

Windows 10: How to list files that doesn't have a file with same name but with different extension?

Fastest way to delete difference of Files with same name, but different filename extension in Windows

delete the older file with the same name but different extension

How to read tables in multiple docx files in a same folder by python

Read specific data from multiple files in different directories python

How to read multiple files from different folder in python

How to read multiple json files stored in a folder into different dictionaries in Python?

Rename all files with the same extension and any name

Fastest way to read last line within pattern of multiple files with the same extension

Renaming multiple files with same name

Saving multiple files with same name

Python script to copy different files from one folder to different sub-folders.with same name as file name

adb pull multiple files with the same extension

Renaming multiple files at once, with the same extension

TOP Ranking

  1. 1

    Failed to listen on localhost:8000 (reason: Cannot assign requested address)

  2. 2

    Loopback Error: connect ECONNREFUSED 127.0.0.1:3306 (MAMP)

  3. 3

    How to import an asset in swift using Bundle.main.path() in a react-native native module

  4. 4

    pump.io port in URL

  5. 5

    Compiler error CS0246 (type or namespace not found) on using Ninject in ASP.NET vNext

  6. 6

    BigQuery - concatenate ignoring NULL

  7. 7

    ngClass error (Can't bind ngClass since it isn't a known property of div) in Angular 11.0.3

  8. 8

    ggplotly no applicable method for 'plotly_build' applied to an object of class "NULL" if statements

  9. 9

    Spring Boot JPA PostgreSQL Web App - Internal Authentication Error

  10. 10

    How to remove the extra space from right in a webview?

  11. 11

    java.lang.NullPointerException: Cannot read the array length because "<local3>" is null

  12. 12

    Jquery different data trapped from direct mousedown event and simulation via $(this).trigger('mousedown');

  13. 13

    flutter: dropdown item programmatically unselect problem

  14. 14

    How to use merge windows unallocated space into Ubuntu using GParted?

  15. 15

    Change dd-mm-yyyy date format of dataframe date column to yyyy-mm-dd

  16. 16

    Nuget add packages gives access denied errors

  17. 17

    Svchost high CPU from Microsoft.BingWeather app errors

  18. 18

    Can't pre-populate phone number and message body in SMS link on iPhones when SMS app is not running in the background

  19. 19

    12.04.3--- Dconf Editor won't show com>canonical>unity option

  20. 20

    Any way to remove trailing whitespace *FOR EDITED* lines in Eclipse [for Java]?

  21. 21

    maven-jaxb2-plugin cannot generate classes due to two declarations cause a collision in ObjectFactory class

HotTag

Archive