So it's a little hard to read (Reddit kinda sucks for formatting code I know), but if I'm reading it right I think I have a solution. You want to wrap your "problem" code in a try block to catch the exception: import sys from pathlib import Path listr = [i for i in range(10)] listr.append('d:\\refresh\\personal_projects\\pcep\\pcep_pt2\\finaltestprep2.py') def linux_interaction(): assert ('linux' in sys.platform), "Function can only run on Linux systems." print('Doing something.') for i in listr: try: if type(i) == str: Path(i).read_text() else: result = i / 0 print('Ok') except FileNotFoundError: print('File not Found') except ZeroDivisionError: print('Divided by zero') except TypeError: print('Cant divide string and int') except AssertionError: print('Linux only function') If I'm missing something let me know because It's a little hard to figure out what your intent is due to the formatting. Answer from chadicus-gigo on reddit.com
🌐
Python
docs.python.org › 3 › library › exceptions.html
Built-in Exceptions — Python 3.14.4 documentation
The path to any file which triggered the exception. Changed in version 3.3: Added the name and path attributes. ... A subclass of ImportError which is raised by import when a module could not be located. It is also raised when None is found in sys.modules.
🌐
Stack Overflow
stackoverflow.com › questions › 73049021 › python-exception-when-a-path-is-not-found
Python exception when a Path is not found - Stack Overflow
Note for additional information - I went through the Python exception hierarchy but unfortunately, I don't see any exception for PathNotFound and SoftwareVersionNotFound scenarios related error. ... What code do you have right now? You'd use os.path.isdir(...) to figure out if ... exists and is a directory. ... As you can see here, you could use OSError that can handle also I/O failures such as “file not found”. You can also use another built-in exception like FileNotFoundError.
Discussions

Why isnt my except error catching the filenotfound error?
So it's a little hard to read (Reddit kinda sucks for formatting code I know), but if I'm reading it right I think I have a solution. You want to wrap your "problem" code in a try block to catch the exception: import sys from pathlib import Path listr = [i for i in range(10)] listr.append('d:\\refresh\\personal_projects\\pcep\\pcep_pt2\\finaltestprep2.py') def linux_interaction(): assert ('linux' in sys.platform), "Function can only run on Linux systems." print('Doing something.') for i in listr: try: if type(i) == str: Path(i).read_text() else: result = i / 0 print('Ok') except FileNotFoundError: print('File not Found') except ZeroDivisionError: print('Divided by zero') except TypeError: print('Cant divide string and int') except AssertionError: print('Linux only function') If I'm missing something let me know because It's a little hard to figure out what your intent is due to the formatting. More on reddit.com
🌐 r/learnpython
18
4
October 10, 2023
Python FileNotFound - Stack Overflow
I am fairly new to python. I am trying to make a script that will read sudoku solutions and determent if they are correct or not. Things I need: 1] Prompt the user to enter a file/file path which More on stackoverflow.com
🌐 stackoverflow.com
Im trying to install Python, yet it keeps saying "Python was not found;". I installed it as a path, manually installed it as a path, help.
You must realize that what you've given us so far amounts to nothing. We don’t know the context,what os you're using, how you tried to install,basically, nothing actionable. Wanna try that again? More on reddit.com
🌐 r/learnpython
31
4
October 26, 2024
Node.js: Python not found exception due to node-sass and node-gyp - Stack Overflow
Suddenly in one of my jenkins environment build has started failing, while in local machine it seems to be working fine as i have python installed, From the logs i was able to detect that the prob... More on stackoverflow.com
🌐 stackoverflow.com
🌐
W3Schools
w3schools.com › python › python_ref_exceptions.asp
Python Built-in Exceptions
Python Overview Python Built-in Functions Python String Methods Python List Methods Python Dictionary Methods Python Tuple Methods Python Set Methods Python File Methods Python Keywords Python Exceptions Python Glossary
🌐
LabEx
labex.io › tutorials › python-how-to-handle-file-not-found-error-in-python-397683
How to handle file not found error in Python | LabEx
The most common and recommended way to handle the FileNotFoundError in Python is by using a try-except block. This allows you to gracefully catch the exception and execute alternative code when the file is not found.
🌐
Reddit
reddit.com › r/learnpython › why isnt my except error catching the filenotfound error?
r/learnpython on Reddit: Why isnt my except error catching the filenotfound error?
October 10, 2023 -

I divided by zero on purpose. However I made a string file that doesn't exist, and when I go to the except clause it is not catching, it keeps terminating the program instead of catching the exception. Why is that?

for i in listr:
if type(i) == str:
    Path(i).read_text()
try:
    i = i/0
except FileNotFoundError:
    print('File not Found ')
except ZeroDivisionError:
    print('Divided by zero')

🌐
GeeksforGeeks
geeksforgeeks.org › python › why-am-i-getting-a-filenotfounderror-in-python
How to fix FileNotFoundError in Python - GeeksforGeeks
April 28, 2025 - FileNotFoundError is a built-in Python exception that is raised when an operation such as reading, writing or deleting is attempted on a file that does not exist at the specified path.
Find elsewhere
🌐
Python documentation
docs.python.org › 3 › tutorial › errors.html
8. Errors and Exceptions — Python 3.14.4 documentation
If an exception occurs which does not match the exception named in the except clause, it is passed on to outer try statements; if no handler is found, it is an unhandled exception and execution stops with an error message.
🌐
Real Python
realpython.com › ref › builtin-exceptions › filenotfounderror
FileNotFoundError | Python’s Built-in Exceptions – Real Python
FileNotFoundError is a built-in exception that occurs when an operation requires a file or directory that doesn’t exist in the specified location. It’s a subclass of OSError designed to signal that the file or directory you’re trying to access couldn’t be found. You can use this exception to handle cases where your code relies on external files or directories that might not be present.
🌐
Python
docs.python.org › 3.3 › library › exceptions.html
5. Built-in Exceptions — Python 3.3.7 documentation
This applies only to unqualified names. The associated value is an error message that includes the name that could not be found. ... This exception is derived from RuntimeError.
🌐
Python.org
discuss.python.org › core development
ValueNotFoundError - Core Development - Discussions on Python.org
July 28, 2023 - I propose adding ValueNotFoundError as a subclass of ValueError so that not found cases can be differentiated from other sources of value errors. This is the same general idea as when we added ModuleNotFoundError as a s…
🌐
DEV Community
dev.to › abidcomsian › how-to-fix-python-was-not-found-error-on-windows-disable-app-execution-aliases-2mj6
How to Fix “Python Was Not Found” Error on Windows: Disable App Execution Aliases - DEV Community
July 24, 2025 - "Python was not found; run without arguments to install from the Microsoft Store, or disable this shortcut from Settings > Apps > Advanced app settings > App execution aliases."
🌐
Python.org
discuss.python.org › python help
Python not found by command prompt - Python Help - Discussions on Python.org
February 11, 2024 - I have installed Python in Windows 11 and saved a small script called unicode.replacer.py In the command prompt, in the correct directory, when I attempt to execute: python unicode.replacer.py command prompt reports: Python was not found. Any suggestions welcome.
🌐
w3resource
w3resource.com › python-exercises › python-exception-handling-exercise-3.php
Python Program for Handling FileNotFoundError
However, if a FileNotFoundError exception occurs because the file does not exist, the program jumps to the except block. In the except block, we handle the exception by printing an error message indicating that the file was not found.
🌐
Real Python
realpython.com › ref › builtin-exceptions › modulenotfounderror
ModuleNotFoundError | Python’s Built-in Exceptions – Real Python
ModuleNotFoundError is a built-in exception that Python raises when an import statement fails to find a specified module. This exception is a subclass of ImportError and it provides a more specific error message when a module can’t be found.
🌐
UW PCE
uwpce-pythoncert.github.io › ProgrammingInPython › modules › Exceptions.html
Exception Handling — Programming in Python 7.0 documentation
In this case, it’s opening and processing a file. But if the file isn’t there, then a FileNotFoundError is “raised”. When an Exception is raised, no further code is run – so the process() function will not be called. Once an exception is raised, Python looks for an except line.
🌐
CodeWithHarry
codewithharry.com › blogpost › solving-python-not-found-run-without-arguments
[Solved] python was not found; run without arguments to install from the microsoft store, or... | Blog | CodeWithHarry
April 5, 2025 - I bought my new Asus laptop and after trying to run Python I encountered the "Python was not found; run without arguments to install from the Microsoft Store or disable this shortcut from Settings > manage app execution aliases" error. If you too have recently faced this issue, I have found a simple solution.
🌐
Top Bug Net
topbug.net › home › catching filenotfounderror? watch out!
Catching FileNotFoundError? Watch Out! - Top Bug Net
October 3, 2020 - def process_file(path): import sys try: f = open(path, 'r') # or os.remove(path) except FileNotFoundError as e: print(f"File {path} not found!", file=sys.stderr) return # process the file...