Last used directory?

Collapse
This topic is closed.
X
X
 
  • Time
  • Show
Clear All
new posts
  • Michael Yanowitz

    #1

    Last used directory?

    Hello:

    Is there a global or some trick I can use to have
    Python remember the last directory visited?
    What I mean is suppose I have this function:

    def get_filename():
    """ Returns a filename selected from a Tkinter File Selection Dialog """
    strFilename = tkFileDialog.as kopenfilename(i nitialdir='.',
    filetypes=[('Python
    files','*.py'),
    ('All
    Files','*.*')])
    return strFilename

    but instead of having initialdir='.' (current directory), I
    would like it set to the last visited directory, which can be from a
    previous run or even a previous day. Is that possible? If so how?


    Thanks in advance:
    Michael Yanowitz

  • Steven D'Aprano

    #2
    Re: Last used directory?

    On Wed, 12 Jul 2006 07:29:18 -0400, Michael Yanowitz wrote:
    but instead of having initialdir='.' (current directory), I
    would like it set to the last visited directory, which can be from a
    previous run or even a previous day. Is that possible? If so how?
    Every time you open a file, save the directory in a global variable. Then
    instead of passing '.' as initialdir, pass that saved directory.

    That will work while you're in the same session. To remember the last
    visited directory from one session to the next, you'll need to write it to
    some sort of permanent storage like a configuration file.

    You'll also need to check what happens if the saved directory no longer
    exists.


    --
    Steven.

    Comment

    Working...