Search code examples
jsonpython-3.xoperating-systemexport-to-csvglob

csv file isn't saved in different directory in python


My code reads a bunch of json files from a directory and extract "frequency" and "attenuation" data from those files and write to a csv file. Now I want to save that csv file in a different directory. The code executes without any error but saves in the current directory. Can anyone help to resolve this issue?

import csv
import glob
import json
import os

site = 'alpha'
frequency_to_check = '196050.000'
json_dir_name = 'V:/temp/test/'
json_pattern = os.path.join(json_dir_name, '*.json')
total_files = glob.glob(json_pattern)
atten = []
timestamp = []
save_path = 'V:/python/result/'
if not os.path.isdir(save_path):
    os.makedirs(save_path)
filename = f'{site}-{frequency_to_check}.csv'
with open(filename, 'w', newline='') as csv_file:
    for file in total_files:
        with open(file) as json_file:
            output_json = json.load(json_file)
            for key in output_json:
                if key['start-freq'] == frequency_to_check:
                    csv.writer(csv_file).writerow([key['start-freq'], key['attenuation']])
    save_file = os.path.join(save_path, filename)
    csv_file.close()

print(f'Total files processed {len(total_files)}')

Solution

  • The issue as far as I can deduce is here :

    csv.writer(csv_file).writerow([key['start-freq'], key['attenuation']])
    

    csv_file is your object that is loaded into memory , and everytime this line is executed you are just writing the rows in the already open file. After that you are just creating a new path :

    save_file = os.path.join(save_path, filename)
    

    which is never really used as you close the file too.

    To fix this I would suggest that you put save_path as csv file :

    import csv
    import glob
    import json
    import os
    
    site = 'alpha'
    frequency_to_check = '196050.000'
    json_dir_name = 'V:/temp/test/'
    json_pattern = os.path.join(json_dir_name, '*.json')
    total_files = glob.glob(json_pattern)
    atten = []
    timestamp = []
    save_path = 'V:/python/result/'
    if not os.path.isdir(save_path):
        os.makedirs(save_path)
    filename = f'{site}-{frequency_to_check}.csv'
    save_file = os.path.join(save_path, filename)
    with open(save_file, 'w', newline='') as csv_file:
        for file in total_files:
            with open(file) as json_file:
                output_json = json.load(json_file)
                for key in output_json:
                    if key['start-freq'] == frequency_to_check:
                        csv.writer(csv_file).writerow([key['start-freq'], key['attenuation']])
        
        csv_file.close()
    
    print(f'Total files processed {len(total_files)}')
    

    I guess this should work.