Yes, for 'import' that's what I would recommend. Though most of my code uses things like below when imports aren't going to necessarily be used ...Surely that's exactly what python's try: ... except: ... mechanism is for? Properly used there's no need to check if the file exists just trap the the ImportError exception:I wish someone would tell Python programmers that. I don't expect programmers to check for every potential error there could be but it seems accepted practice to not even check for the presence of a file before opening it and the like. That can lead to pages of Traceback when a simple "File 'file.ext' does not exist", "Module 'name' is not installed", would be much clearer and more useful.
Code:
try : import serialexcept : serial = NoneFor a 'with open' any 'except' may be well down the source code so I prefer -
Code:
if not os.path.isfile(filename): Failed("Cannot find", filename)with open(filename, "r") as f: ...Especially for Python where almost no programmers seem to do it. Noobs then just do the same, have few role models to guide them.The difficult part, especially with self taught or lazy programmers is getting them to do it.
Statistics: Posted by hippy — Tue Dec 16, 2025 1:55 pm