OSDN Git Service

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