如何扫描文本文件的目录并按修改日期读取文本文件,将其打印到屏幕让脚本每5秒扫描一次目录以便对新文件进行creadted并打印。 是否有可能你可以帮助我我卡住了,我需要这个真正的坏,我已经得到了扫描目录文件和打印,但它不打印日期更新的文件。文件修改和操作
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
什么,我想是我的代码,而不是询问是否要打印,我需要它来打印文件,如果当前的时间,如果它读取只是放在目录和新文件意味着修改一个进来它会读取新文件而不会提示我。 它给我的错误是强制unicode:需要字符串或缓冲区,找到列表。
你的问题不清楚。 “按修改日期阅读文本文件”是什么意思?你的意思是你想按修改时间顺序打印所有文件吗?或者,您是否只想打印自上次查看文件后发生更改的文件? – 2012-02-17 18:12:57
你使用的是Linux吗?如果是这样,有一个更好的方法来监视文件系统事件的目录:[pyinotify](http://pyinotify.sourceforge.net/)。对于[OSX](http://en.wikipedia.org/wiki/FSEvents)和[Windows](http://stackoverflow.com/questions/3517460/is-there-anything-like-inotify-在窗口/ 3517475#3517475)。 – unutbu 2012-02-17 18:14:41
请不要发布[确切的重复问题](http://stackoverflow.com/questions/9332264/file-modification-and-creation)。这是非常不礼貌的。 – 2012-02-17 18:23:23