Opening and closing a large number of files on python
<p>I'm writing a program which organizes my school mark and for every subject I created a file.pck where are saved all the marks of that subject. Since I have to open and pickle.load 10+ files I decided to make 2 functions, files_open():</p>

<pre><code>subj1 = open(subj1_file)
subj1_marks = pickle.load(subj1)
subj2 = open(subj2_file)
subj2marks = pickle.load(subj2)
</code></pre>

<p>and file_close():</p>

<pre><code>subj1.close()
subj2.close()
</code></pre>

<p>The problem is that I had to make every variable in file_open() global and the function now is too long. I tried to avoid that problem by calling variables like:</p>

<pre><code>file_open.subj1
</code></pre>

<p>but it does work and I can't understand why.</p>

<p>since you just want to open, load and close the file afterwards I would suggest a simple helper function:</p>

<pre><code>def load_marks(filename):
    with open(filename,"rb") as f:  # don't forget to open as binary
         marks = pickle.load(f)
    return marks
</code></pre>

<p>Use like this:</p>

<pre><code>subj1_marks = load_marks(subj1_file)
</code></pre>

<p>The file is closed when going out of scope of <code>with</code> block, and your data remains accessible even if the file is closed which may be your (unjustified) concern with your question.</p>

<p>Note: someone suggested that what you really want (maybe) is to save all your data in one big pickle file.
In that case, you could create a dictionary containing your data:</p>

<pre><code>d = dict()
d["mark1"] = subj1_marks
d["mark2"] = subj2_marks
...
</code></pre>

<p>and perform one sole <code>pickle.dump()</code> and <code>pickle.load()</code> on the dictionary (if data is picklable then a dictionary of this data is also picklable): that would be simpler to handle 1 big file than a lot of them, knowning that you need all of them anyway.</p>

python