Re: A switch somewhere, or bug? CORRECTION

2025-12-06 Thread Thomas Passin

On 12/6/2025 2:22 AM, Michael Torrie via Python-list wrote:

On Sat, Dec 6, 2025, 00:04 Chris Angelico  wrote:


On Sat, 6 Dec 2025 at 15:52, Michael Torrie via Python-list
 wrote:


On 12/5/25 6:36 PM, Chris Angelico via Python-list wrote:

On Sat, 6 Dec 2025 at 12:33, Michael Torrie via Python-list
 wrote:

Starter = open("HLYlog.txt","w");
filepath = Starter.name


Isn't that just...

filepath = "HLYlog.txt"


yup.  But did you miss the os.path.abspath() bit?



I didn't, and that part IS useful and relevant, but the opening of the
file just adds potential failure points without really adding
anything.



On the contrary it gives him a file to look for to prove what was
happening.


The real point is that if the file doesn't get created, you don't know 
whether the program failed or it was never run. The console will flash 
into existence and then go away so quickly that one can't read any text 
that may have been emitted.


If you really think that the program ran but failed to create the file, 
then put the file creation in try..except, in the except block print a 
message about what happened, and then pause the program in one of the 
ways posted earlier so you can read the message.


Continuing to run the program the same way without diagnostics isn't 
going to help.

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


Re: A switch somewhere, or bug? CORRECTION

2025-12-06 Thread Thomas Passin

On 12/6/2025 2:51 PM, Em wrote:


The confusing part here is that no one has indicated whether or not the
statement fails in any of the six ways to run this program except me.

Am I the only one that as the problem?


No.  I created the program with the suggested printout of the working 
directory.  Here's what I ran:


import os
print("Start")
print("working directory:", os.getcwd())
input("   Starter file")

try:
Starter = open("HLY-LOG5.txt","w")
except Exception as e:
print(f"Error: {e}")
input("End")

It ran but failed to create the file.  That's because of the working 
directory when the program was run by double-clicking its name. Here's 
the output.


Start
working directory: C:\WINDOWS\system32
   Starter file
Error: [Errno 13] Permission denied: 'HLY-LOG5.txt'
End

An ordinary user doesn't have permission for the C:\WINDOWS\system32 
directory.  Maybe on your Windows 10 system the working directory was 
the program's directory. But with the new file association system it's 
not.  I think it should be, myself.


When I ran the program by typing just its name in a terminal opened on 
the program's directory, it created the file without any errors.


I'm running Python 3.12+, but the exact version won't matter because the 
initial working directory is set by Windows, not Python.


If you really really want programs that reliably write to the program's 
directory, you can get it from sys.argv[0] and have Python change 
directories for you. Otherwise, start your program in some other way 
than by double-clicking.



-Original Message-
From: Peter J. Holzer 
Sent: Saturday, December 6, 2025 12:56 PM
To: [email protected]
Subject: Re: A switch somewhere, or bug? CORRECTION

On 2025-12-04 18:03:34 -0500, Em wrote:
^^
Weird. That was before most of the thread (and the message wasn't stuck
in a moderation queue), yet it seems everybody ignored this message. .
Probably because it isn't actually part of the thread, but ...


On my computer: Win11, Python 3.14.1

  


Double-click and run on the file".  You will see the first two lines
execute.

The screen will show "Start" and "Starter File".


Good. So we know that the program is executed.


Hit  to resolve the
pause statement and the program crashes when it tries to create the
"HLY-LOG5" file. The last line, "End" is not printed. No file is created.


So the next step is to find out why it isn't executed. First print the
working directory:

import os
print("Start")
print("working directory:", os.getcwd())
input("   Starter file")

then wrap the open in a try/except block:

try:
 Starter = open("HLY-LOG5.txt","w")
except Exception as e:
 print("Error: {e}")
input("End")

This should now tell you in which directory the program is actually trying
to create the file and why it didn't work.

 hip



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


Re: A switch somewhere, or bug?

2025-12-06 Thread Pierre Fortin via Python-list
On Thu, 4 Dec 2025 11:06:21 -0500 Em wrote:

>Two computers, both alike in dignity, in fair windows land...
>
>in Win10/Python 3.13 my program runs as well as can be expected. However, on
>my new computer Win11/Python 3.14.1 there is a peculiarity.
>
>On the new machine, when I click on the file name, it immediately dumps out.
>I go in with IDLE and it runs fine with no indication of any error.
>
>I believe this line is the culprit:
>Starter = open(HLYlog.txt", "w")
>
>Thoughts appreciated.

I asked claude.ai...

Question:  any reason this python statement works on windows10; but fails
on windows11 when called from cmd; yet works on both from IDLE:
open("file.txt","w")

This is an interesting permission/behavior difference between Windows
versions and execution contexts. Here are the most likely culprits: 

1. Controlled Folder Access (Windows Defender)

Windows 11 has more aggressive "Controlled Folder Access" settings by
default. When running from cmd.exe, Python might not be in the allowed
apps list, blocking write access to protected folders (like Documents,
Desktop, etc.).

Check: Windows Security → Virus & threat protection → Ransomware
protection → Controlled folder access 

2. Working Directory Differences

From cmd, Python's working directory is wherever cmd is currently located
(could be C:\Windows\System32 or your user folder). IDLE typically
defaults to a different location.

Try in cmd:

python -c "import os; print(os.getcwd())"

If it's trying to write to System32 or another protected location, that
would explain the failure. 

3. UAC/Permissions Changes

Windows 11 has stricter UAC enforcement. The cmd session might be running
with different privileges than IDLE.

Test: Right-click cmd → "Run as administrator" and try again.

4. Antivirus/Real-time Protection

Some antivirus software treats cmd-spawned processes differently than GUI
apps like IDLE.

What's the actual error message? That would narrow this down immediately:

PermissionError: [Errno 13] → Permissions/Controlled Folder Access
FileNotFoundError → Working directory issue
Other errors → Different root cause

Quick diagnostic: Try open("C:\\Temp\\file.txt", "w") from cmd (assuming
C:\Temp exists) to see if it's directory-specific.

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


Re: A switch somewhere, or bug? CORRECTION

2025-12-06 Thread Roel Schroeven

Op 6/12/2025 om 20:51 schreef Em:

The confusing part here is that no one has indicated whether or not the
statement fails in any of the six ways to run this program except me.

Am I the only one that as the problem?
Sometimes it happens to me too that a script doesn't work, for whatever 
reason. What I then do is launch the script in a way that maximizes my 
chances to discover what's going wrong. E.g. I run the script from a 
command prompt. Have you done that yet? Have you seen my other post 
(from 2025-12-05)? Have you tried my suggestions? What where the results?


Frankly I have to say you're making it difficult for us to help you. 
Troubleshooting is a matter of investigation, trying things out, 
analyzing the results. Each situation is different, each system is 
different, so we can't do that for you. And you're not really following 
up on our suggestions for ways to troubleshoot. Why are you making 
things so difficult for use, and hence for yourself as well?


--
"Man had always assumed that he was more intelligent than dolphins because
he had achieved so much — the wheel, New York, wars and so on — whilst all
the dolphins had ever done was muck about in the water having a good time.
But conversely, the dolphins had always believed that they were far more
intelligent than man — for precisely the same reasons."
-- Douglas Adams


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


Re: A switch somewhere, or bug? CORRECTION

2025-12-06 Thread Peter J. Holzer
On 2025-12-04 18:03:34 -0500, Em wrote:
   ^^
   Weird. That was before most of the thread (and the message wasn't
stuck in a moderation queue), yet it seems everybody ignored this message. .
Probably because it isn't actually part of the thread, but ...

> On my computer: Win11, Python 3.14.1
> 
>  
> 
> Double-click and run on the file".  You will see the first two lines
> execute.
> 
> The screen will show "Start" and "Starter File".

Good. So we know that the program is executed.

> Hit  to resolve the
> pause statement and the program crashes when it tries to create the
> "HLY-LOG5" file. The last line, "End" is not printed. No file is created.

So the next step is to find out why it isn't executed. First print the
working directory:

import os
print("Start")
print("working directory:", os.getcwd())
input("   Starter file")

then wrap the open in a try/except block:

try:
Starter = open("HLY-LOG5.txt","w")
except Exception as e:
print("Error: {e}")
input("End")

This should now tell you in which directory the program is actually
trying to create the file and why id didn't work.

hjp

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | [email protected] |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"


signature.asc
Description: PGP signature
-- 
https://mail.python.org/mailman3//lists/python-list.python.org


RE: A switch somewhere, or bug? CORRECTION

2025-12-06 Thread Em


-Original Message-
From: Roel Schroeven  
Sent: Saturday, December 6, 2025 7:00 PM
To: [email protected]
Subject: Re: A switch somewhere, or bug? CORRECTION

Op 6/12/2025 om 20:51 schreef Em:
>> The confusing part here is that no one has indicated whether or not 
>> the statement fails in any of the six ways to run this program except me.
>> Am I the only one that has the problem?

>Sometimes it happens to me too that a script doesn't work, for whatever 
>reason. 

This is not a matter of "sometimes".  This it totally predictable and it 
happens for me every time depending how I run it.

I have narrowed it down to an extremely simple program:

Pause = input(" Start ")
Starter = open("HLY-LOG5.txt","w")
pause - input(" End ")

Out of the seven scenarios:

Four options work if you use:
 WIN10 and double click on the filename.
 WIN10 and open the file with IDLE and press F5.
 WIN11 and open the file with IDLE and press F5.
 WIN11 and activate it from the command prompt.

Three fail to work if you use:
  WIN11 and double-click on the filename.
  WIN11 and select "Open using Python".
  WIN10 and select "Open using Python". (This one totally surprised me.)

>What I then do is launch the script in a way that maximizes my chances to 
>discover >what's going wrong. E.g. I run the script from a command prompt. 
>Have you done >that yet? Have you seen my other post (from 2025-12-05)? Have 
>you tried my >suggestions? What where the results?

Although I am attempting to use the suggestions provided, they do little help 
if I don't know the scope of the problem.

Is it happening only on my computers?
No one, so far, has posted either way.


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

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


Re: A switch somewhere, or bug? CORRECTION

2025-12-06 Thread Thomas Passin

On 12/6/2025 6:49 PM, Em wrote:

For at least the last 10 years. I have been using double click on the
filename (or by shortcut) to use the program. It tracks the Insulin
injections for me and creates/updates several text files. I have been using
IDLE for editing on WIN10 computers.  No issues

When I copied the file to the new WIN11 computer and double-click on the
filename, it fails without warning or explanation.  In WIN11, I can open the
file with IDLE and use F5 to run it successfully.

I was told to try "Open with Python" and it fails on both the WIN10 and
WIN11 computers.  I do not see the option for this program to Run as
Administrator on either computer.  I have seen/used Run as Administrator
elsewhere on the WIN10 computer.

I created a .py program with the lines of code:

pause = input("Start")
Starter = open("HLY-LOG5.txt","w")
pause = input("End")

and can follow all six of the techniques to run as mentioned above.  Three
situations, the program runs, and three have the program fail. Exactly the
same results as with my medical program.

Has my short, three line, program worked on your system?


You should have sent this message to the group, not just me. Yes, your 
program created the file but not when I double-clicked on the file name. 
 As I explained in my last post, that's because in Windows 11 when 
double-clicking, the working directory is the system's Windows 
directory, not the one your program is in. In Win 11, you don't have 
access to it as an ordinary user.  Anyway even if you did, you don't 
want to write your file there.


There's a simple solution if you want to be able to launch by double 
clicking the file.  Actually, there are at least four ways to go.


1. Run your program using a batch file.  in the batch file, cd to your 
target directory before launching your program.  The batch file needs to 
be somewhere on your path, or alternatively you can put a shortcut to it 
on your desktop.


If you don't know how to do any of those things, ask for help.

2. Make your Python program change directories to the target directory 
before writing the file. I think someone already posted a code snippet 
showing how to do that. If not, and you don't know how, ask for help.


3. Hard-code the full path to your target file.  Then it won't matter 
what working directory is in effect.


4.Create a shortcut for your .py file on the desktop. Then open the 
Properties dialog for the shortcut. In the "Shortcut" tab you can put 
the desired working directory.  Now when you double click the shortcut, 
it will open in the right directory.



-Original Message-
From: Thomas Passin 
Sent: Saturday, December 6, 2025 4:17 PM
To: [email protected]
Subject: Re: A switch somewhere, or bug? CORRECTION

On 12/6/2025 2:51 PM, Em wrote:


The confusing part here is that no one has indicated whether or not
the statement fails in any of the six ways to run this program except me.

Am I the only one that as the problem?


No.  I created the program with the suggested printout of the working
directory.  Here's what I ran:

import os
print("Start")
print("working directory:", os.getcwd())
input("   Starter file")

try:
  Starter = open("HLY-LOG5.txt","w")
except Exception as e:
  print(f"Error: {e}")
input("End")

It ran but failed to create the file.  That's because of the working
directory when the program was run by double-clicking its name. Here's the
output.

Start
working directory: C:\WINDOWS\system32
 Starter file
Error: [Errno 13] Permission denied: 'HLY-LOG5.txt'
End

An ordinary user doesn't have permission for the C:\WINDOWS\system32
directory.  Maybe on your Windows 10 system the working directory was the
program's directory. But with the new file association system it's not.  I
think it should be, myself.

When I ran the program by typing just its name in a terminal opened on the
program's directory, it created the file without any errors.

I'm running Python 3.12+, but the exact version won't matter because the
initial working directory is set by Windows, not Python.

If you really really want programs that reliably write to the program's
directory, you can get it from sys.argv[0] and have Python change
directories for you. Otherwise, start your program in some other way than by
double-clicking.


-Original Message-
From: Peter J. Holzer 
Sent: Saturday, December 6, 2025 12:56 PM
To: [email protected]
Subject: Re: A switch somewhere, or bug? CORRECTION

On 2025-12-04 18:03:34 -0500, Em wrote:
 ^^
 Weird. That was before most of the thread (and the message wasn't
stuck in a moderation queue), yet it seems everybody ignored this message.

.

Probably because it isn't actually part of the thread, but ...


On my computer: Win11, Python 3.14.1

   


Double-click and run on the file".  You will see the first two lines
execute.

The screen will show "Start" and "Starter File".


Good. So we know that the program is executed.


Hit  to resolve the

RE: A switch somewhere, or bug? CORRECTION

2025-12-06 Thread Em


The confusing part here is that no one has indicated whether or not the
statement fails in any of the six ways to run this program except me.

Am I the only one that as the problem?

-Original Message-
From: Peter J. Holzer  
Sent: Saturday, December 6, 2025 12:56 PM
To: [email protected]
Subject: Re: A switch somewhere, or bug? CORRECTION

On 2025-12-04 18:03:34 -0500, Em wrote:
   ^^
   Weird. That was before most of the thread (and the message wasn't stuck
in a moderation queue), yet it seems everybody ignored this message. .
Probably because it isn't actually part of the thread, but ...

> On my computer: Win11, Python 3.14.1
> 
>  
> 
> Double-click and run on the file".  You will see the first two lines 
> execute.
> 
> The screen will show "Start" and "Starter File".

Good. So we know that the program is executed.

> Hit  to resolve the
> pause statement and the program crashes when it tries to create the 
> "HLY-LOG5" file. The last line, "End" is not printed. No file is created.

So the next step is to find out why it isn't executed. First print the
working directory:

import os
print("Start")
print("working directory:", os.getcwd())
input("   Starter file")

then wrap the open in a try/except block:

try:
Starter = open("HLY-LOG5.txt","w")
except Exception as e:
print("Error: {e}")
input("End")

This should now tell you in which directory the program is actually trying
to create the file and why it didn't work.

hip

-- 
   _  | Peter J. Holzer| Story must make more sense than reality.
|_|_) ||
| |   | [email protected] |-- Charles Stross, "Creative writing
__/   | http://www.hjp.at/ |   challenge!"

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


Re: A switch somewhere, or bug? CORRECTION

2025-12-06 Thread MRAB



On 07/12/2025 00:54, Thomas Passin wrote:

On 12/6/2025 6:49 PM, Em wrote:

For at least the last 10 years. I have been using double click on the
filename (or by shortcut) to use the program. It tracks the Insulin
injections for me and creates/updates several text files. I have been 
using

IDLE for editing on WIN10 computers.  No issues

When I copied the file to the new WIN11 computer and double-click on the
filename, it fails without warning or explanation.  In WIN11, I can 
open the

file with IDLE and use F5 to run it successfully.

I was told to try "Open with Python" and it fails on both the WIN10 and
WIN11 computers.  I do not see the option for this program to Run as
Administrator on either computer.  I have seen/used Run as Administrator
elsewhere on the WIN10 computer.

I created a .py program with the lines of code:

pause = input("Start")
Starter = open("HLY-LOG5.txt","w")
pause = input("End")

and can follow all six of the techniques to run as mentioned above.  
Three
situations, the program runs, and three have the program fail. 
Exactly the

same results as with my medical program.

Has my short, three line, program worked on your system?


You should have sent this message to the group, not just me. Yes, your 
program created the file but not when I double-clicked on the file 
name.  As I explained in my last post, that's because in Windows 11 
when double-clicking, the working directory is the system's Windows 
directory, not the one your program is in. In Win 11, you don't have 
access to it as an ordinary user.  Anyway even if you did, you don't 
want to write your file there.


There's a simple solution if you want to be able to launch by double 
clicking the file.  Actually, there are at least four ways to go.


1. Run your program using a batch file.  in the batch file, cd to your 
target directory before launching your program.  The batch file needs 
to be somewhere on your path, or alternatively you can put a shortcut 
to it on your desktop.


If you don't know how to do any of those things, ask for help.

2. Make your Python program change directories to the target directory 
before writing the file. I think someone already posted a code snippet 
showing how to do that. If not, and you don't know how, ask for help.


3. Hard-code the full path to your target file.  Then it won't matter 
what working directory is in effect.


4.Create a shortcut for your .py file on the desktop. Then open the 
Properties dialog for the shortcut. In the "Shortcut" tab you can put 
the desired working directory.  Now when you double click the 
shortcut, it will open in the right directory.



[snip]

I always find a script's location with `__file__`.

If "HLY-LOG5.txt" is in the same folder as the script, then its path is 
given by:


    from os.path import dirname, join

    log_path = join(dirname(__file__), "HLY-LOG5.txt")

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