On 2/27/26 7:49 AM, Em wrote:
> 
> The statement is: ThisPath = os.getcwd()
> 
> For 10 years in Win 10 the line gave me the present path both when I used F5 
> in an editor, and when I ran the program by a double-click on the name of the 
> program in the folder.
> 
> I now want to run the program in Win 11.
> As in Win 10, F5 from an editor, it gives me the present path but when I 
> double-click on the program in the folder, it gives me some system path on 
> the C drive.

I thought we went through all this with you in December where thought
there was a bug in Python.

> This is now the second line of code that fails like this.
> Can someone explain why.

Yes it was explained several times to you. Google Gemini can give you a
better answer (seriously it can) that you can interact with.  AI is very
useful for this sort of query.

> Can you suggest a line of code to get the present path for this that is 
> allowed by Win 11?

If by "this" you mean open the file in the same directory as the py
script itself, this is a very common thing.  Can be done with one line
of code, or two if you want it to be more clear.  In fact it's so common
that any free AI can answer this quickly. Here's Google Gemini's version:

import os

# Get the absolute path to the directory where the script lives
script_dir = os.path.dirname(os.path.abspath(__file__))

# Change the current working directory to that path
os.chdir(script_dir)

# Verification
print(f"Current working directory: {os.getcwd()}")

Another commonly-used possibility is to create a windows shortcut to the
python launcher exe, and then in the shortcut settings set the working
directory ("start in" field)  you want it to use, and also the path to
the script you want it to launch as an argument.  This is pretty much
the canonical way to force the working directory to something you want.

-- 
https://mail.python.org/mailman3//lists/python-list.python.org

Reply via email to