Quantcast
Viewing all articles
Browse latest Browse all 47

Answer by philberndt for How do I check whether a file exists without exceptions?

You could try this (safer):

try:    # http://effbot.org/zone/python-with-statement.htm    # 'with' is safer to open a file    with open('whatever.txt') as fh:        # Do something with 'fh'except IOError as e:    print("({})".format(e))

The ouput would be:

([Errno 2] No such file or directory:'whatever.txt')

Then, depending on the result, your program can just keep running from there or you can code to stop it if you want.


Viewing all articles
Browse latest Browse all 47

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>