1# -*- coding: utf-8 -*- 2 3import os 4 5def open_file_or_fd(val, *argl, **kwargs): 6 """ 7 If 'val' looks like a decimal integer, open it as an fd. If not, try to 8 open it as a regular file. 9 """ 10 11 fd = -1 12 try: 13 # Does it look like an integer? 14 fd = int(val, 10) 15 except ValueError: 16 pass 17 18 # Try to open it... 19 if fd != -1: 20 return os.fdopen(fd, *argl, **kwargs) 21 else: 22 return open(val, *argl, **kwargs) 23