1

I have been trying to create a program that will allow the user to create a file where they will store their notes. I have managed to get this part down but now I have run into a problem with loading all the files as variables that the rest of the program can readily access. I have managed to find 2 ways to allow this to happen.

One is to call each file one at a time:

with open("file_name", "r") as working_notes:
    file_name = json.load(working_notes)

... # and repeat for every file that needs to be opened.

as you could imaging this would become a bit to maintain and would require the code to be updated every time a new file was created. So great in the short term to just get the job done but really bad going forward.

The 2nd method was to use tuple packing/unpacking:

vzt_notes, rms_notes, nsr_notes, py_notes,\
vzt_keys, rms_keys, nsr_keys, py_keys, java_notes,\
java_keys= load_files(filenames) 
# calls a function that opens all files related to each variable

This actually works and all I need to do is add a variable name to the tuple list and it works fine however this still requires me to edit the code each time a file is created. Its a lot more compact and partially automated with the call of the function but I need complete automation.

This is where I am getting stuck. after trying various loops I have written or trying to create the variables from a list (failed miserable) I can not seam to have all the files load when the program initiates as a variable name of the file name.

So my latest attempt has only gotten as far as being able to print out ever files content but I still am not sure how to take the filename of the file being opened and making it a variable with the same name as the file name so the rest of the program can see and work with said variable.

Here is what I tried last:

import os

path = 'C:/Users/mcdomi3/Documents/MINTworkspace/MINT/NotesKeys/'

for filename in os.listdir(path):
    with open(path+filename, "r+") as filename:
        filename = json.load(filename)
        # print (filename)

as I said this last attempt has only gotten as far as to print the content of the files listed just to see if it is in fact doing something with the files. So I know I can read them at least during the for loop.

Mike - SMT
  • 14,784
  • 4
  • 35
  • 79
  • 2
    Instead of using separate variables for each file, why not creating a `dict` with the filename as key and the contents as value? This way instead of accessing `rms_notes` you could access e.g. `notebook["rms_notes"]` for the contents. – Christian König Apr 26 '17 at 15:04

1 Answers1

1

I would use a dict to store the data from the unknown number of notebook files:

import os
import json

path = 'C:/Users/mcdomi3/Documents/MINTworkspace/MINT/NotesKeys/'

notebook = dict()

for filename in os.listdir(path):
    with open(path+filename, "r+") as filehandle:
        notebook[filename] = json.load(filehandle)

Afterwards you can access the contents of each notebook by notebook["file_name_of_notebook"].

If you really need variables, try this approach with globals():

>>> new_name #variable not existing yet
Traceback (most recent call last):
  File "<pyshell#18>", line 1, in <module>
    new_name
NameError: name 'new_name' is not defined
>>> globals()["new_name"]="new value"
>>> print(new_name)
new value
Christian König
  • 3,437
  • 16
  • 28
  • So the files contain a single `dict` within them and the notes are stored as a `keyword : "string of notes"`. So for example if I wanted a separate not library of all things related to my job I would have a file named job_notes made. The way my program works is you can select your library of notes from a drop down list and this allows you to switch between different category of notes on the fly. – Mike - SMT Apr 26 '17 at 15:18
  • They way my program reads all the data is all through `variables` that are `=` to the `dict` inside the file being loaded. If I change how the data is being read I will have to rewrite the entire program to compensate. – Mike - SMT Apr 26 '17 at 15:20
  • I'm sorry, but I'm afraid i don't understand you. Maybe a look at this question can help you: http://stackoverflow.com/questions/1373164/how-do-i-create-a-variable-number-of-variables – Christian König Apr 26 '17 at 15:21
  • added another solution – Christian König Apr 26 '17 at 15:26
  • I am looking at your first option right now. It is going to take a bit to test it as I need to rewrite about 150 lines of code to then look for the data in a different way then I currently have it set up. I will be back at some point (today most likely) With the results of the attempt. Thanks :) – Mike - SMT Apr 26 '17 at 15:29
  • Quick question. After I pull all the data into a single `dict` is it possible to then write all that data back to the files it was pulled from? or would I have to create one big file to store it all on? – Mike - SMT Apr 26 '17 at 15:32
  • Sure you can save everything in separate files, if you want to. Your code should work the same as before, if you change each occurence of "variable_name" with notebook["variable_name"]. – Christian König Apr 26 '17 at 17:47
  • 1
    Ya I did figure that out after I asked about it. After rewriting about 150 lines of code I did manage to get it to work in the manor I was trying to get done. So thank you for the advice on putting it all into a single `dict` :). I don't think 150 is a lot on average however being my 1st and only program I have ever made, for me it was like rewriting the entire thing. XD – Mike - SMT Apr 26 '17 at 17:54