FAQ
Hi all, can anybody tell me whether there's a way to change the default
location for files to be opened by open()? I'd like to be able to create
files somewhere other than my Python folder without having to write the
full path in the filename every time. Sorry if this is a stupid
question, I don't know much about programming.

Search Discussions

  • Krister Svanlund at Jan 24, 2010 at 3:14 pm

    On Sun, Jan 24, 2010 at 4:08 PM, Rotwang wrote:
    Hi all, can anybody tell me whether there's a way to change the default
    location for files to be opened by open()? I'd like to be able to create
    files somewhere other than my Python folder without having to write the full
    path in the filename every time. Sorry if this is a stupid question, I don't
    know much about programming.
    Check out http://docs.python.org/library/os.html and the function
    chdir it is what you are looking for.
  • Rotwang at Jan 24, 2010 at 3:49 pm

    Krister Svanlund wrote:
    On Sun, Jan 24, 2010 at 4:08 PM, Rotwang wrote:
    Hi all, can anybody tell me whether there's a way to change the default
    location for files to be opened by open()? I'd like to be able to create
    files somewhere other than my Python folder without having to write the full
    path in the filename every time. Sorry if this is a stupid question, I don't
    know much about programming.
    Check out http://docs.python.org/library/os.html and the function
    chdir it is what you are looking for.
    Thank you. So would adding

    import os
    os.chdir(<path>)

    to site.py (or any other module which is automatically imported during
    initialisation) change the default location to <path> every time I used
    Python?
  • Christian Heimes at Jan 24, 2010 at 4:01 pm

    Rotwang wrote:
    import os
    os.chdir(<path>)

    to site.py (or any other module which is automatically imported during
    initialisation) change the default location to <path> every time I used
    Python?
    First of all you shouldn't alter the site module ever! The optional
    sitecustomize module exists to make global changes.

    A library must never change the current working directory. It's up to
    the application to choose the right working directory. If you mess with
    the working directory in a library or global module like site you *will*
    break applications. Python has multiple ways to modify the list of
    importable locations, either globally, for the current user or the
    current application. Use them wisely!

    Christian
  • Rotwang at Jan 24, 2010 at 4:33 pm

    Christian Heimes wrote:
    Rotwang wrote:
    import os
    os.chdir(<path>)

    to site.py (or any other module which is automatically imported during
    initialisation) change the default location to <path> every time I used
    Python?
    First of all you shouldn't alter the site module ever! The optional
    sitecustomize module exists to make global changes.

    A library must never change the current working directory. It's up to
    the application to choose the right working directory. If you mess with
    the working directory in a library or global module like site you *will*
    break applications. Python has multiple ways to modify the list of
    importable locations, either globally, for the current user or the
    current application. Use them wisely!

    Christian
    OK, thanks.
  • Günther Dietrich at Jan 24, 2010 at 6:04 pm

    Rotwang wrote:

    Check out http://docs.python.org/library/os.html and the function
    chdir it is what you are looking for.
    Thank you. So would adding

    import os
    os.chdir(<path>)

    to site.py (or any other module which is automatically imported during
    initialisation) change the default location to <path> every time I used
    Python?
    Don't change the library modules. It would catch you anytime when you
    expect it least.

    See for the environment variable PYTHONSTARTUP and the associated
    startup file.



    Best regards,

    G?nther
  • Gabriel Genellina at Jan 25, 2010 at 7:47 pm
    En Sun, 24 Jan 2010 15:04:48 -0300, G?nther Dietrich
    <gd_usenet at spamfence.net> escribi?:
    Rotwang wrote:
    Check out http://docs.python.org/library/os.html and the function
    chdir it is what you are looking for.
    Thank you. So would adding

    import os
    os.chdir(<path>)

    to site.py (or any other module which is automatically imported during
    initialisation) change the default location to <path> every time I used
    Python?
    Don't change the library modules. It would catch you anytime when you
    expect it least.

    See for the environment variable PYTHONSTARTUP and the associated
    startup file.
    sitecustomize.py would be a better place. PYTHONSTARTUP is only used when
    running in interactive mode.
    Anyway, I'd do that explicitely on each script that requires it; after
    upgrading the Python version, or moving to another PC, those scripts would
    start failing...

    --
    Gabriel Genellina
  • Nobody at Jan 30, 2010 at 4:01 pm

    On Sun, 24 Jan 2010 15:08:15 +0000, Rotwang wrote:

    Hi all, can anybody tell me whether there's a way to change the default
    location for files to be opened by open()? I'd like to be able to create
    files somewhere other than my Python folder without having to write the
    full path in the filename every time. Sorry if this is a stupid question,
    I don't know much about programming.
    If you pass a relative pathname to open() (or any other function which
    expects a filename), it will be interpreted relative to the current
    directory.

    Given that the current directory always seems to be the Python directory,
    and you refer to it as a "folder", I'm guessing that you're running Python
    on Windows via a shortcut in the Start Menu or on the desktop.

    In which case, the ideal solution is probably to open the Properties
    dialog for the shortcut and change the "Start in" field to your
    "My Documents" directory (or some subdirectory of it).

    Python itself won't care which directory it starts in.
  • Aahz at Jan 30, 2010 at 6:59 pm
    In article <hjhnp0$7bc$1 at news.eternal-september.org>,
    Rotwang wrote:
    Hi all, can anybody tell me whether there's a way to change the default
    location for files to be opened by open()? I'd like to be able to create
    files somewhere other than my Python folder without having to write the
    full path in the filename every time. Sorry if this is a stupid
    question, I don't know much about programming.
    from os.path import join
    BASE = '/path/to/root'
    f = open(join(BASE, filename))

    Trust me, you'll be much happier with this.
    --
    Aahz (aahz at pythoncraft.com) <*> http://www.pythoncraft.com/

    import antigravity

Related Discussions

Discussion Navigation
viewthread | post
Discussion Overview
grouppython-list @
categoriespython
postedJan 24, '10 at 3:08p
activeJan 30, '10 at 6:59p
posts9
users7
websitepython.org

People

Translate

site design / logo © 2022 Grokbase