Re: A switch somewhere, or bug? CORRECTION

Thomas Passin <[email protected]> Sat, 6 Dec 2025 16:16:45 -0500
Newsgroups gmane.comp.python.general
Message-ID <[email protected]>
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 <[email protected]>
> 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 <Enter> 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
>