0

I have generated following list of file names in my python code

json_list = ['myfile.1.json', 'myfile.10.json', 'myfile.100.json', 'myfile.101.json','myfile.2.json']

I would like to sort it according to the digits because in the end I would like to merge the file content based on the order of the filename.

How can I sort this list as following.

json_list = ['myfile.1.json', ,'myfile.2.json', 'myfile.10.json', 'myfile.100.json', 'myfile.101.json']

I have following python code.

#join multiple files

# absolute path to search all json files inside a specific folder
path = r'*.json'
json_list = glob.glob(path)
print(json_list)  #above list

data = ""

for file_name in json_list :
    with open(file_name ,"r", encoding='utf-8') as file_handle :
        temp_data = file_handle.read()
        data = data + temp_data 

with open ('merged.json', 'a', encoding='utf-8') as file_handle : 
    file_handle.write(data)

rshar
  • 1,381
  • 10
  • 28

0 Answers0