-1

How would you scan a dir for a text file and read the text file by date modified, print it to screen having the script scan the directory every 5 seconds for a newer file creadted and prints it. Is it possible that you can help me i'm stuck and i need this real bad and i've already got the scan dir for file and print but it does not print the files by date modidfied.

import os,sys
os.chdir(raw_input('dir_path: ') )    
contents=os.listdir('.') #contents of the current directory
files =[]
directory=[]
Time = time.ctime(os.path.getmtime(contents))
for i in contents:
    if os.path.isfile(i) == True :
       files.append(i)
    elif os.path.isdir(i) == True :
       directory.append(i)
    #printing contents
choice = ""       
for j in files:
    while choice != "quit":
            choice = raw_input("Dou you want to print file  %s (y/n): "%j)
            if choice == 'y':
               print "**************************"
               print "Printing Files %s" %j
               print "**************************"
               fileobj = open(j,'r')
               contents = fileobj.readlines()
               for k in contents:
                     sys.stderr.write(k)
               else:
                    pass

what i wanted is instead of my code asking if it wants to print i need it to print the files if modified by the current time meaning if it read a file that was just placed in the directory and a new one comes in it will read the new file without prompting me. the error it's giving me is coercing to unicode: need string or buffer, list found.

mintgreen
  • 895
  • 1
  • 6
  • 6
  • Your question is unclear. what do you mean by "read the text file by date modified"? Do you mean you want to print all files in order of modification time? Or do you only want to print files which have changed since the last time to looked at the file? – Li-aung Yip Feb 17 '12 at 18:12
  • Are you using linux? If so, there is a better way to monitor a directory for filesystem events: [pyinotify](http://pyinotify.sourceforge.net/). There are similar solutions for [OSX](http://en.wikipedia.org/wiki/FSEvents) and [Windows](http://stackoverflow.com/questions/3517460/is-there-anything-like-inotify-on-windows/3517475#3517475). – unutbu Feb 17 '12 at 18:14
  • 1
    Please don't post [exact duplicate questions](http://stackoverflow.com/questions/9332264/file-modification-and-creation). It is extremely impolite. – Li-aung Yip Feb 17 '12 at 18:23
  • i only want to print the file which were changed last – mintgreen Feb 17 '12 at 19:11

1 Answers1

0

Repeating actions on a timer

You can repeat an action every five seconds by combining an infinite loop with the time.sleep() function, like so:

import time
while True:
    time.sleep(5)         # wait five seconds
    print (time.time())   # print the time

Remember to have some kind of break condition in here if you need it, otherwise the loop will run forever.

"TypeError: coercing to Unicode: need string or buffer, list found"

Your problem is in the line

Time = time.ctime(os.path.getmtime(contents))

You have provided a list of filenames. The os.path.getmtime function expects one filename at a time. The error message is telling you that it has no idea how to convert a list of filenames into a filename.

Li-aung Yip
  • 12,320
  • 5
  • 34
  • 49
  • i mean printing the files in the dir one by one by modified time? – mintgreen Mar 06 '12 at 15:15
  • `contents`, as returned by `os.listdir()`, is a sequence of filenames. Meditate on how you could use a `for` loop to go through that sequence element by element. – Li-aung Yip Mar 06 '12 at 18:00