<?xml version="1.0" encoding="UTF-8"?>
<rss version="2.0" xmlns:atom="http://www.w3.org/2005/Atom" xmlns:dc="http://purl.org/dc/elements/1.1/">
  <channel>
    <title>DEV Community: Aneesh Katla</title>
    <description>The latest articles on DEV Community by Aneesh Katla (@an33shk4tl4).</description>
    <link>https://dev.to/an33shk4tl4</link>
    <image>
      <url>https://media2.dev.to/dynamic/image/width=90,height=90,fit=cover,gravity=auto,format=auto/https:%2F%2Fdev-to-uploads.s3.amazonaws.com%2Fuploads%2Fuser%2Fprofile_image%2F38796%2Fc36f02a1-1c3f-4eea-87c8-5f2a2d6a7abc.png</url>
      <title>DEV Community: Aneesh Katla</title>
      <link>https://dev.to/an33shk4tl4</link>
    </image>
    <atom:link rel="self" type="application/rss+xml" href="https://dev.to/feed/an33shk4tl4"/>
    <language>en</language>
    <item>
      <title>Python Generator Example : File Watcher</title>
      <dc:creator>Aneesh Katla</dc:creator>
      <pubDate>Thu, 26 Oct 2017 11:49:17 +0000</pubDate>
      <link>https://dev.to/an33shk4tl4/python-generator-example--file-watcher-jo</link>
      <guid>https://dev.to/an33shk4tl4/python-generator-example--file-watcher-jo</guid>
      <description>&lt;h2&gt;
  
  
  Python - Generators ==&amp;gt; Demo using file watcher
&lt;/h2&gt;



&lt;div class="highlight js-code-highlight"&gt;
&lt;pre class="highlight plaintext"&gt;&lt;code&gt;import os
from datetime import datetime as dt
import time

def checkfile(filename):
    try:
        st = os.stat(filename)
        fmtime = st.st_mtime
    except Exception as e:
        print e.message
        return
    print "*********init modification timestamp" , dt.fromtimestamp(fmtime).ctime()
    while True:
        print 'entered while loop'
        st = os.stat(filename)
        print 'collected file stats'
        yield dt.fromtimestamp(fmtime).ctime()
        print 'yield executed'
        if dt.fromtimestamp(st.st_mtime).ctime() &amp;lt;&amp;gt; dt.fromtimestamp(fmtime).ctime():
            print "File modification changed - new file modification timestamp", dt.fromtimestamp(fmtime).ctime()
            fmtime = st.st_mtime
            print 'set new mod time inside if condition'
        else:
            print 'Mod timestamps equal'
        time.sleep(2)

chk = checkfile(r'C:\filetowatch.txt')

for i in chk:
    print chk.next()
    print '---for loop next iteration after gen call'
&lt;/code&gt;&lt;/pre&gt;

&lt;/div&gt;



&lt;p&gt;Python has an interesting feature called generators which allow you to consume the benefits of a an iterator but without memory overhead.Generally, an iterator is an object in memory with a list of objects. I've written a simple code above which demonstrates how a generator works. You'll need to create another script to delete and re-create a file in order to see this working.&lt;/p&gt;

&lt;h3&gt;
  
  
  What's that code?
&lt;/h3&gt;

&lt;p&gt;The code simply watches for file modified time and identifies if there is any change in the file. A simple file watcher program. I've used python base libraries and hence no external dependencies.&lt;/p&gt;

&lt;h3&gt;
  
  
  Learning
&lt;/h3&gt;

&lt;p&gt;File created time won't work (atleast in windows) for the same filename because Windows does not change the create time.Check out this &lt;a href="https://support.microsoft.com/en-us/help/172190/windows-nt-contains-file-system-tunneling-capabilities"&gt;article&lt;/a&gt; for more info on "name" tunneling &lt;/p&gt;

&lt;p&gt;Feel free to leave your feedback...&lt;/p&gt;

</description>
      <category>python</category>
      <category>generator</category>
      <category>filewatcher</category>
    </item>
  </channel>
</rss>
