[Python] When debugging with Pycharm, code that should not have been executed was executed

Discussão em 'Python' iniciado por Stack, Novembro 27, 2024.

  1. Stack

    Stack Membro Participativo

    When I was debugging Python code with Pycharm, I encountered a strange problem where a function that should not have been executed was executed. Here is my code:

    import os

    input_dir = "D:/MyCode/Data/t"

    for root, dirs, files in os.walk(input_dir):
    for file in files:
    if file.endswith(".jpg") and '-' in file:
    if file == "x":
    print("error")


    I don't have the file x in my folder, but when I set a breakpoint in the print ("error") code, print ("error") executes and outputs an error. If I set breakpoints at if file. endscan (". jpg") and '-' in file:, print ("error") will not be executed, and when I run the code without debugging, print ("error") will not be executed either. May I ask what caused this?

    Location of breakpoint setting [​IMG]

    results of execution [​IMG]

    This is a reproduced image of my problem. I set a breakpoint at the print function, but the print function that should not have been executed was executed instead, and the program did not exit properly

    I tried to change the files in the folder, but even if there were no files in my folder, the print function would still be executed.

    Sorry, I may not have explained my question clearly. What I mean is that in my code logic, if file == "x": should never be true, so print("error") should not be executed. However, during breakpoint debugging, when I place a breakpoint at print("error"), the console outputs "error" and the program exits with exit code -1073741819 (0xC0000005). What could be the reason for this?

    I changed the output to file, mask.png is clearly not equal to x, but the console still outputs mask.png.

    [​IMG]

    [​IMG]

    Continue reading...

Compartilhe esta Página