Mike - SMT
Mike - SMT

Reputation: 15236

Opening files by using a list?

EDIT: The way I wrote the question did not seam to get the answer I was looking for. Here is a shorter version of the question I think is more clear.

I want to take the following code and condense it int a single for loop.

with open("vzt_notes", "r") as working_vzt_notes:
    vzt_notes = json.load(working_vzt_notes)
with open("rms_notes", "r") as working_rms_notes:
    rms_notes = json.load(working_rms_notes)
with open("nsr_notes", "r") as working_nsr_notes:
    nsr_notes = json.load(working_nsr_notes)
with open("py_notes", "r") as working_py_notes:
    py_notes = json.load(working_py_notes)
with open("vzt_keys", "r") as vzt_kw:
    vzt_keys = json.load(vzt_kw)
with open("rms_keys", "r") as rms_kw:
    rms_keys = json.load(rms_kw)
with open("nsr_keys", "r") as nsr_kw:
    nsr_keys = json.load(nsr_kw)
with open("py_keys", "r") as py_kw:
    py_keys = json.load(py_kw)

Currently I open all of my files one at a time. As I am going to be creating many more files dynamically I want to have a for loop that will open all my files in the manor I am doing above. I have attempted a few for loops however all I can get to somewhat work is storing all the dictionaries from the files being opened into one big dictionary. I don't want to do it this way. I need to have the results from the for loop perform the same task as opening each file one at a time and naming the variable to be the same as the file name and storing the files contents in that variable.

Upvotes: 1

Views: 75

Answers (3)

Harvey
Harvey

Reputation: 5821

It looks like you're trying to add new variables to the global scope programmatically. Why not store your notes in a dictionary instead? Here's a method to achieve what you want.

import json

def json_loader(filename):
    with open(filename) as f:
        return json.load(f)


def load_files(filenames):
    return (json_loader(filename) for filename in filenames)


filenames = [
    'vzt_notes', 'rms_notes', 'nsr_notes',
    'py_notes', 'vzt_keys', 'rms_keys',
    'nsr_keys', 'py_keys',
]

vzt_notes, rms_notes, nsr_notes, \
    py_notes, vzt_keys, rms_keys, \
    nsr_keys, py_keys = load_files(filenames)

print(list(load_files(filenames)))

With the above approach, it may be better (safer) for maintaining your code to do this:

vzt_notes = json_loader('vzt_notes')
rms_notes = json_loader('rms_notes')
...

Upvotes: 4

sgrg
sgrg

Reputation: 1240

Iterate over the file names, open each one, append the loaded file to a list which the function returns.

file_list =[ "vzt_notes","rms_notes","nsr_notes","py_notes","vzt_keys","rms_keys","nsr_keys","py_keys" ]

def loadFiles(event=None):
    loaded_file_list = []
    for filename in file_list:
        # You can reuse the file handle f
        with open(filename, "r") as f:
            loaded_file_list.append(json.load(f))
    return loaded_file_list

loaded_files = loadFiles()

Upvotes: 0

Alex Hall
Alex Hall

Reputation: 36043

Here is what I think you're trying to achieve:

for file_suffix in file_list:
    prefix = "working_"
    filename = prefix + file_suffix
    with open(filename, "r") as file_obj:
        data = json.load(file_obj)

All I've done is change variable names so that they don't clash.

Upvotes: 1

Related Questions