summaryrefslogtreecommitdiff
path: root/tools/python/xen/util.py
blob: 47ceb5bd21fe9ab8019c445d5a00ee80cc922bbf (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
# -*- 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)