how to use an open file for reuse it in severals functions?
<p>I am a beginner in python and not completely bilingual, so I hope you understand me. I'm trying to develop a code where anyone can open a file, in order to display its contents in a graph matplotlib, to do this using a function called <code>read_file()</code> with which I get the data and insert a <code>Listbox</code> without any problems. I accomplished the functionality but my concern arises when I want to call the information contained in the file from another function called <code>show_graph()</code>, in this part I require use the loaded file  (in the <code>read_file()</code> function), the only way to achieve this is by adding:</p>

<pre><code>f = open(âexample1.las')

log = LASReader(f, null_subs=np.nan)
</code></pre>

<p>with which I can plot, but not practical for me, in other words how to use an open file for reuse it in severals functions? </p>

<p>Someone could give me their support to solve this please?</p>

<p>Here is the complete code:</p>

<pre><code>from Tkinter import *
from las import LASReader
from pprint import pprint
import tkFileDialog
import matplotlib, sys
matplotlib.use('TkAgg')
import numpy as np
from matplotlib.backends.backend_tkagg import FigureCanvasTkAgg, NavigationToolbar2TkAgg
from matplotlib.figure import Figure
import matplotlib.pyplot as plt

root = Tk()
root.geometry("900x700+10+10")

def read_file():
    filename = tkFileDialog.askopenfilename()
    f = open(filename)
    log = LASReader(f, null_subs=np.nan)

    for curve in log.curves.names:
    parent.insert(END,curve)

def add_name():
    it = parent.get(ACTIVE)
    child.insert(END, it)

def show_graph():
    child = Listbox(root, selectmode=MULTIPLE)
    try:
        s = child.selection_get()
        if s == "GR":
            print 'selected:', s            
            f = open('example1.las')
            log = LASReader(f, null_subs=np.nan)


            fig = plt.figure(figsize=(6, 7.5))

            plt.plot(log.data['GR'], log.data['DEPT'])
            plt.ylabel(log.curves.DEPT.descr + " (%s)" % log.curves.DEPT.units)
            plt.xlabel(log.curves.GR.descr + " (%s)" % log.curves.GR.units)
            plt.ylim(log.stop, log.start)
            plt.title(log.well.WELL.data + ', ' + log.well.DATE.data)
            plt.grid()
            dataPlot = FigureCanvasTkAgg(fig, master=root)
            dataPlot.show()
            dataPlot.get_tk_widget().grid(row=0, column=2, columnspan=2, rowspan=2,
                sticky=W+E+N+S, padx=380, pady=52)

        elif s == "NPHI":
            print 'selected:', s
            f = open('Shamar-1.las')
            log = LASReader(f, null_subs=np.nan) 


            fig = plt.figure(figsize=(6, 7.5))

            plt.plot(log.data['NPHI'], log.data['DEPT'])
            plt.ylabel(log.curves.DEPT.descr + " (%s)" % log.curves.DEPT.units)
            plt.xlabel(log.curves.NPHI.descr + " (%s)" % log.curves.NPHI.units)
            plt.ylim(log.stop, log.start)
            plt.title(log.well.WELL.data + ', ' + log.well.DATE.data)
            plt.grid()
            dataPlot = FigureCanvasTkAgg(fig, master=root)
            dataPlot.show()
            dataPlot.get_tk_widget().grid(row=0, column=2, columnspan=2, rowspan=2,
                sticky=W+E+N+S, padx=380, pady=52)

        elif s == "DPHI":
            print 'selected:', s
            f = open('Shamar-1.las')
            log = LASReader(f, null_subs=np.nan) 


            fig = plt.figure(figsize=(6, 7.5))

            plt.plot(log.data['DPHI'], log.data['DEPT'])
            plt.ylabel(log.curves.DEPT.descr + " (%s)" % log.curves.DEPT.units)
            plt.xlabel(log.curves.DPHI.descr + " (%s)" % log.curves.DPHI.units)
            plt.ylim(log.stop, log.start)
            plt.title(log.well.WELL.data + ', ' + log.well.DATE.data)
            plt.grid()
            dataPlot = FigureCanvasTkAgg(fig, master=root)
            dataPlot.show()
            dataPlot.get_tk_widget().grid(row=0, column=2, columnspan=2, rowspan=2,
                sticky=W+E+N+S, padx=380, pady=52)

    except:
        print 'no selection'

def remove_name():
    child.delete(ACTIVE)

def btnClick():
        pass

e = Entry(root)
e.pack(padx=5)
b = Button(root, text="OK", command=btnClick)
b.pack(pady=5)

# create the canvas, size in pixels
canvas = Canvas(width = 490, height = 600, bg = 'grey')
# pack the canvas into a frame/form
canvas.place(x=340, y=50)

etiqueta = Label(root, text='Nemonics:')
etiqueta.place(x=10, y=30)

parent = Listbox(root)

root.title("Viewer")

parent.place(x=5, y=50)

selec_button = Button(root, text='Graph',
                    command=show_graph)
selec_button.place(x=340, y=20)

remove_button = Button(root, text='&lt;&lt;delete',
                    command=remove_name)
remove_button.place(x=138, y=150)

add_button = Button(root, text='Add&gt;&gt;',
                    command=add_name)
add_button.place(x=138, y=75)


child = Listbox(root)
child.place(x=210, y=50)

butt = Button(root, text="load file", command=read_file)
butt.place(x=10, y=5)



root.mainloop()
</code></pre>

<p>You can use a global variable to keep it, declaring it as global before the variable name (f in your case). However, I don't recommend it if you are modifying the file.</p>

python tkinter