How to write this json file?

I know this question is not directly related, But in my pytorch model I need to create this json file first.

import glob
import json
import os

LABEL = {
    'neu': '01',  #: 'neutral',
    'fru': '02',  #: 'calm',
    'hap': '03',  #: 'happy',
    'sad': '04',  #: 'sad',
    'ang': '05',  #: 'angry',
    'fea': '06',  #: 'fearful',
    'exc': '07',  #: 'disgust',
    'sur': '08',  #: 'surprised'
    'xxx': '09',  #: 'other'
}


PATH_TXT = glob.glob("E:/dataset/data/IEMOCAP_full_release/*/dialog/EmoEvaluation/S*.txt")
PATH_WAV = glob.glob("E:/dataset/data/IEMOCAP_full_release/*/sentences/wav/*/S*.wav")

PAIR = {}


def getPair():
    for path in PATH_TXT:
        with open(path, 'r') as f:
            fr = f.read().split("\t")
            for i in range(len(fr)):
                if (fr[i] in LABEL):
                    PAIR[fr[i - 1]] = fr[i]


def rename():
    for i in PATH_WAV:
        for j in PAIR:
            if (os.path.basename(i)[:-4] == j):
                k = j.split('_')
                if (len(k) == 3):
                    name = os.path.dirname(i) + '/' + k[0] + '-' + k[1] + '-' + LABEL[PAIR[j]] + '-01-' + k[2] + '.wav'
                    os.rename(src=i, dst=name)
                    print(name)
                    '''
                    Ses01F_impro01_F000.wav
                    k[0]:Ses01F
                    k[1]:impro01
                    k[2]:F000
                    Ses01F-impro01-XX-01-F000.wav
                    '''
                elif (len(k) == 4):
                    name = os.path.dirname(i) + '/' + k[0] + '-' + k[1] + '-' + LABEL[PAIR[j]] + '-01-' + k[2] + '_' + \
                           k[3] + '.wav'
                    os.rename(src=i, dst=name)
                    print(name)
                    '''
                    Ses03M_script03_2_F032.wav
                    k[0]:Ses03M
                    k[1]:script03
                    k[2]:2
                    k[3]:F032
                    Ses03M-script03-XX-01-2_F032.wav
                    '''


if __name__ == '__main__':
    pairPath = "E:/Python_On_All_Dataset/IEMO/IEMOCAP"
    # if (os.path.exists(pairPath)):
    #     with open(pairPath, 'r') as f:
    #         PAIR = json.load(f)
    # else:
    getPair()
    with open('my.json', 'w') as f:
        json.dump(obj=PAIR, fp=f)
rename()

when I run this ''main" function its throwing an error: saying permission error

Now I am unable to run and regenerate the error because the second fun rename() is changing the filenames in my original dataset. So plz guide how to change this code at line

os.rename(src=i, dst=name)


so that the renamed files are stored in some other location, then probably I can save this dict name PAIR in json format.

Regards

I’m not if I understand the issue correctly, but guess you might change the dst location to store the files to another location.
In any case, since this is unrelated to PyTorch I would recommend to post it on StackOverflow or any other general Python discussion board to get a faster and better answer.

1 Like

@ptrblck thank you, sir, actually I have already posted there no reply yet.

In that case could you explain the problem a bit more?
It seems you’ve executed the script, some files were moved, but the script failed with a permission error (did you solve the permission issue?).
Afterwards you wanted to continue the script, but are now running into issues since a subset was already moved?

sir I am trying your previous suggestion.just changing dst lets see if it works for me. I will let you know sir.
regards

@ptrblck Hi sir, I have changed this dst to the required different folder as 'E:\\Python_On_All_Dataset\\IEMO\\long_codes\\multiscale_att_impro_only\\head_fusion-master\\IEMOCAP'. But its throwing the error:

[WinError 183] Cannot create a file when that file already exists: 'E:/dataset/data/IEMOCAP_full_release\\Session1\\sentences\\wav\\Ses01F_impro01\\Ses01F_impro01_F000.wav' -> 'E:\\Python_On_All_Dataset\\IEMO\\long_codes\\multiscale_att_impro_only\\head_fusion-master\\IEMOCAP'

Sir let me explain it again: Here I want to change the data files name which are stored in original directory of my dataset, and renamed files need to be stored at other location so that my original data files are not disturbed. But why is it saying these files already exist there. The destination folder IEMOCAP is empty :frowning:
Regards