[Tutor] What is the easiest way to ensure the current working directory is the same as the directory where the main program is saved?

boB Stepp robertvstepp at gmail.com
Fri Jun 25 23:50:52 EDT 2021


I have asked questions on this topic off and on in the past.  So far I
have been able to make my programs do what I intend, but I have a
feeling I am still not getting something fundamental.

On Fri, Jun 25, 2021 at 10:21 PM Cameron Simpson <cs at cskk.id.au> wrote:

> [...]
> >Is there an _easy_ way to have a program start with its current
> >working directory in the same directory where its source code resides
> >no matter from where the program is launched?  Or must I always use
> >the __file__ attribute to determine where the source code resides and
> >then change directories from there?
>
> The latter.
>
> But I recommend _not_ changing your working directory. It is a process
> global state which affects everything. When someone invokes your
> programme and supplies a filename on the command line, they usually
> expect that name to be resolved from the working directory where they
> invoked the programme.

I never considered this possibility!  Yes, must avoid doing that!!

> If the change directory you need to know where you were if you need to
> work with any caller suppplied relative filenames.
>
> >How do people deal with their
> >Python applications that in theory may be installed anywhere in a
> >user's file system?  There must be some standard way of dealing with
> >this that I am too dense to uncover.
>
> Well, broadly you don't care where the code is. You care about the data
> of the person using your programme, which they'll tell you. (Directly
> with filenames or the like, or also by envionment variables.)

<Scratch head>  But I *do* care about where my source code winds up,
don't I?  How else do I load data from its data folder if I don't know
where I am in the user's file system?

> When you do care there are two basic approaches that come to mind:
>
> - use __file__, get its dirname, and access some resources you know are
>   installed beside the source; this is generally awkward - you need
>   control during install, and it is possible to ship python source as a
>   zip file and the __file__ paradigm doesn't work there
>
> - have a config file specifying the location of resources

Maybe this is where my misunderstandings are occurring.  To date, I
have had no installation process.  Any programs that I need to be
elsewhere, I copy the program to its new living facilities, whether
elsewhere on my PC, somewhere else on a network, or to another PC.
Does an actual installation process make these issues go away?  As a
user of other people's programs that come with an installer one of the
first choices the user usually must make is accept the default
installation suggestion or choose a different location.  I suppose
this is where the needed information is fed into the program and
stored for later use?

I thought I could put off figuring out how to install Python packages
the "proper" way (if there is one) ...

> Maybe you could enumerate some circumstances giving you trouble. See
> what can be done.

A typical example from my earlier playing-around-with-this session --
needing to open and read in another file:

Play around code:
------------------------
import os

print("Testing!")
print("Current working directory: ", os.getcwd())
print("Source code file: ", __file__)
with open('test_text.txt') as f:
    print(f.read())
------------------------

Now if I run this from the same folder where the source code and data
file are, all is fine.  But if I start the program elsewhere then a
problem ensues:

PS C:\> Users\boB\Practical_Programming\testing.py    # NOT the
program directory.
Testing!
Current working directory:  C:\
Source code file:  C:\Users\boB\Practical_Programming\testing.py
Traceback (most recent call last):
  File "C:\Users\boB\Practical_Programming\testing.py", line 6, in <module>
    with open('test_text.txt') as f:
FileNotFoundError: [Errno 2] No such file or directory: 'test_text.txt'
PS C:\> cd .\Users\boB\Practical_Programming\
PS C:\Users\boB\Practical_Programming> .\testing.py    # Now in the
program's folder.
Testing!
Current working directory:  C:\Users\boB\Practical_Programming
Source code file:  C:\Users\boB\Practical_Programming\testing.py
This is just a plain text file of no import.
This is the second line of a boring file.

Though a toy example, this is the basic problem I have been hacking
around.  My usual approach is when I am lazy I hard-code the "home"
location for wherever I place the program.  When I am more energetic I
make use of the __file__ attribute to automatically decide the "home"
location.

How should I be handling these situations?

Cheers!
boB Stepp


More information about the Tutor mailing list