Convenience function to remove a possibly non-existant file

This commit is contained in:
Neal Norwitz 2006-01-23 07:51:27 +00:00
parent 9730bcb4a6
commit 0e17f8cd38

View File

@ -49,23 +49,24 @@ def unload(name):
except KeyError: except KeyError:
pass pass
def unlink(filename):
import os
try:
os.unlink(filename)
except OSError:
pass
def forget(modname): def forget(modname):
'''"Forget" a module was ever imported by removing it from sys.modules and '''"Forget" a module was ever imported by removing it from sys.modules and
deleting any .pyc and .pyo files.''' deleting any .pyc and .pyo files.'''
unload(modname) unload(modname)
import os import os
for dirname in sys.path: for dirname in sys.path:
try: unlink(os.path.join(dirname, modname + os.extsep + 'pyc'))
os.unlink(os.path.join(dirname, modname + os.extsep + 'pyc'))
except os.error:
pass
# Deleting the .pyo file cannot be within the 'try' for the .pyc since # Deleting the .pyo file cannot be within the 'try' for the .pyc since
# the chance exists that there is no .pyc (and thus the 'try' statement # the chance exists that there is no .pyc (and thus the 'try' statement
# is exited) but there is a .pyo file. # is exited) but there is a .pyo file.
try: unlink(os.path.join(dirname, modname + os.extsep + 'pyo'))
os.unlink(os.path.join(dirname, modname + os.extsep + 'pyo'))
except os.error:
pass
def is_resource_enabled(resource): def is_resource_enabled(resource):
"""Test whether a resource is enabled. Known resources are set by """Test whether a resource is enabled. Known resources are set by
@ -175,14 +176,9 @@ except IOError:
(TESTFN, TMP_TESTFN)) (TESTFN, TMP_TESTFN))
if fp is not None: if fp is not None:
fp.close() fp.close()
try: unlink(TESTFN)
os.unlink(TESTFN)
except:
pass
del os, fp del os, fp
from os import unlink
def findfile(file, here=__file__): def findfile(file, here=__file__):
"""Try to find a file on sys.path and the working directory. If it is not """Try to find a file on sys.path and the working directory. If it is not
found the argument passed to the function is returned (this does not found the argument passed to the function is returned (this does not