Files
xen/tools/python/xen/util.py
T
Andrew Cooper c82aff87f1 tools/python: Drop shebangs from library files
These aren't runable scripts, so shouldn't have shebangs.

Signed-off-by: Andrew Cooper <andrew.cooper3@citrix.com>
Acked-by: Marek Marczykowski-Górecki <marmarek@invisiblethingslab.com>
2023-03-17 10:44:16 +00:00

23 lines
467 B
Python

# -*- coding: utf-8 -*-
import os
def open_file_or_fd(val, *argl, **kwargs):
"""
If 'val' looks like a decimal integer, open it as an fd. If not, try to
open it as a regular file.
"""
fd = -1
try:
# Does it look like an integer?
fd = int(val, 10)
except ValueError:
pass
# Try to open it...
if fd != -1:
return os.fdopen(fd, *argl, **kwargs)
else:
return open(val, *argl, **kwargs)