Prev: ctypes for AIX
Next: PS.
From: Rotwang on
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: Krister Svanlund on
On Sun, Jan 24, 2010 at 4:08 PM, Rotwang <sg552(a)hotmail.co.uk> 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.
From: Rotwang on
Krister Svanlund wrote:
> On Sun, Jan 24, 2010 at 4:08 PM, Rotwang <sg552(a)hotmail.co.uk> 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?
From: Christian Heimes on
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

From: Rotwang on
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.
 |  Next  |  Last
Pages: 1 2
Prev: ctypes for AIX
Next: PS.