OSDN Git Service

add wp_imgswap2.py for new OSDN Magazine
[otptools/otptools.git] / deterfile.py
1 # deterfile.py
2 """deterfile.py - determine file type"""
3
4 import os
5 import os.path
6 import re
7 import getjpggeom
8
9 _file_cmd = "file %s"
10
11 def file(path):
12     """
13     determine given file's type.
14     This function returns strings.
15
16     @param path: filepath you want to determine
17     @type path: string
18     """
19     if not os.path.isfile(path):
20         return ("file not exist.",)
21
22     cmd = _file_cmd % path
23     escaped_path = path.replace("\\", "\\\\");
24     stdout = os.popen(cmd, "r")
25     results = []
26     for line in stdout:
27         if re.search(r"^%s:" % escaped_path, line):
28             line = re.sub(r"^%s:\s*" % escaped_path, "", line)
29         tpl = line.strip().split(",")
30         results.extend([x.strip() for x in tpl])
31     stdout.close()
32     return results
33
34 #### test code
35 # import sys
36
37 # tgt = sys.argv[1]
38 # r = file(tgt)
39 # for item in r:
40 #     print item