{"id":2656,"date":"2016-05-14T14:40:48","date_gmt":"2016-05-14T06:40:48","guid":{"rendered":"http:\/\/ixyzero.com\/blog\/?p=2656"},"modified":"2016-05-14T14:40:48","modified_gmt":"2016-05-14T06:40:48","slug":"%e7%94%a8python%e5%ae%9e%e7%8e%b0%e3%80%8ctail-f%e3%80%8d%e7%9a%84%e5%8a%9f%e8%83%bd","status":"publish","type":"post","link":"https:\/\/ixyzero.com\/blog\/archives\/2656.html","title":{"rendered":"\u7528Python\u5b9e\u73b0\u300ctail -f\u300d\u7684\u529f\u80fd"},"content":{"rendered":"<p>=Start=<\/p>\n<h5>\u53c2\u8003\u4ee3\u7801\uff1a<\/h5>\n<pre class=\"lang:default decode:true\">'''\nPython-Tail - Unix tail follow implementation in Python.\npython-tail can be used to monitor changes to a file.\nExample:\n    import tail\n    # Create a tail instance\n    t = tail.Tail('file-to-be-followed')\n    # Register a callback function to be called when a new line is found in the followed file.\n    # If no callback function is registerd, new lines would be printed to standard out.\n    t.register_callback(callback_function)\n    # Follow the file with 5 seconds as sleep time between iterations.\n    # If sleep time is not provided 1 second is used as the default time.\n    t.follow(s=5)\n'''\n\n# Author - Kasun Herath &lt;kasunh01 at gmail.com&gt;\n# Source - https:\/\/github.com\/kasun\/python-tail\n\nimport os\nimport sys\nimport time\n\nclass Tail(object):\n    ''' Represents a tail command. '''\n    def __init__(self, tailed_file):\n        ''' Initiate a Tail instance.\n            Check for file validity, assigns callback function to standard out.\n\n            Arguments:\n                tailed_file - File to be followed. '''\n\n        self.check_file_validity(tailed_file)\n        self.tailed_file = tailed_file\n        self.callback = sys.stdout.write\n\n    def follow(self, s=1):\n        ''' Do a tail follow. If a callback function is registered it is called with every new line.\n        Else printed to standard out.\n\n        Arguments:\n            s - Number of seconds to wait between each iteration; Defaults to 1. '''\n\n        with open(self.tailed_file) as file_:\n            # Go to the end of file\n            file_.seek(0,2)\n            while True:\n                curr_position = file_.tell()\n                line = file_.readline()\n                if not line:\n                    file_.seek(curr_position)\n                else:\n                    self.callback(line)\n                time.sleep(s)\n\n    def register_callback(self, func):\n        ''' Overrides default callback function to provided function. '''\n        self.callback = func\n\n    def check_file_validity(self, file_):\n        ''' Check whether the a given file exists, readable and is a file '''\n        if not os.access(file_, os.F_OK):\n            raise TailError(\"File '%s' does not exist\" % (file_))\n        if not os.access(file_, os.R_OK):\n            raise TailError(\"File '%s' not readable\" % (file_))\n        if os.path.isdir(file_):\n            raise TailError(\"File '%s' is a directory\" % (file_))\n\nclass TailError(Exception):\n    def __init__(self, msg):\n        self.message = msg\n    def __str__(self):\n        return self.message<\/pre>\n<p>\u5355\u6587\u4ef6\u5f15\u7528\u793a\u4f8b\uff08\u5c06\u4e0a\u9762\u7684\u4ee3\u7801\u5b58\u4e3a\u6587\u4ef6\u300ctail.py\u300d\uff09\uff1a<\/p>\n<pre class=\"lang:default decode:true \">import tail\n\n# Create a tail instance\nt = tail.Tail('tail_test.log')  #\u8981\u76d1\u63a7\u7684\u6587\u4ef6\u5fc5\u987b\u5df2\u7ecf\u5b58\u5728\uff0c\u5426\u5219\u4f1a\u629b\u51fa\u5f02\u5e38\n\n# Register a callback function to be called when a new line is found in the followed file.\n# If no callback function is registerd, new lines would be printed to standard out.\n# t.register_callback(callback_function)\n\n# Follow the file with 5 seconds as sleep time between iterations.\n# If sleep time is not provided 1 second is used as the default time.\n# t.follow(s=5)\n\nt.follow()<\/pre>\n<h5>\u66f4\u591a\u53c2\u8003\u94fe\u63a5\uff1a<\/h5>\n<ul>\n<li><a href=\"http:\/\/www.opstool.com\/article\/226\">\u5b9e\u73b0Python\u7248\u7684tail -f\u529f\u80fd<\/a><\/li>\n<li><a href=\"http:\/\/my.oschina.net\/leejun2005\/blog\/167567\">Python \u6a21\u62df linux shell \u4e0b\u7684 tail -f \u52a8\u6001\u6253\u5370\u65e5\u5fd7<\/a><\/li>\n<li><a href=\"http:\/\/www.open-open.com\/lib\/view\/open1392516064772.html\">Python\u52a8\u6001\u76d1\u63a7\u65e5\u5fd7\u7684\u5185\u5bb9<\/a><\/li>\n<li>python\u5b9e\u73b0tail\uff08\u8003\u8651\u5230\u51e0\u79cd\u7279\u6b8a\u60c5\u51b5\uff09\n<ul>\n<li><a href=\"http:\/\/www.cnblogs.com\/bufferfly\/p\/4878688.html\">http:\/\/www.cnblogs.com\/bufferfly\/p\/4878688.html<\/a><\/li>\n<li><a href=\"http:\/\/www.the5fire.com\/python-code-linux-tail.html\">http:\/\/www.the5fire.com\/python-code-linux-tail.html<\/a><\/li>\n<li><a href=\"http:\/\/blog.sina.com.cn\/s\/blog_53bd284b0102vbh4.html\">http:\/\/blog.sina.com.cn\/s\/blog_53bd284b0102vbh4.html<\/a><\/li>\n<li><a href=\"http:\/\/chenzhou123520.iteye.com\/blog\/1963341\">http:\/\/chenzhou123520.iteye.com\/blog\/1963341<\/a><\/li>\n<\/ul>\n<\/li>\n<li><a href=\"http:\/\/blog.csdn.net\/love_chivas\/article\/details\/31939885\">Python\u5b9e\u73b0tail\u547d\u4ee4\uff0c\u5305\u542b-f -n\u53c2\u6570<\/a><\/li>\n<\/ul>\n<p>=END=<\/p>\n","protected":false},"excerpt":{"rendered":"<p>=Start= \u53c2\u8003\u4ee3\u7801\uff1a &#8221;&#8217; Python-Tail &#8211; Unix tail follow implem [&hellip;]<\/p>\n","protected":false},"author":2,"featured_media":0,"comment_status":"open","ping_status":"closed","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[11,7,12],"tags":[8,601],"class_list":["post-2656","post","type-post","status-publish","format-standard","hentry","category-linux","category-programing","category-tools","tag-python","tag-tail"],"views":3369,"_links":{"self":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2656","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/users\/2"}],"replies":[{"embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/comments?post=2656"}],"version-history":[{"count":0,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/posts\/2656\/revisions"}],"wp:attachment":[{"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/media?parent=2656"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/categories?post=2656"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/ixyzero.com\/blog\/wp-json\/wp\/v2\/tags?post=2656"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}