文件修改和创建 [英] file modification and creation
问题描述
如何扫描文本文件的目录并按日期修改读取文本文件,将其打印到屏幕上,每隔5秒钟将脚本扫描目录,以便对较新的文件进行编辑并打印。
有可能你可以帮助我,我被困住,我需要这个真正的坏,我已经拥有扫描目录的文件和打印,但它不会按日期modidfied打印文件。
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
我想要的是代替我的代码,询问是否要打印我需要它打印文件,如果修改当前时间的含义,如果它读取刚刚放在目录中的文件,并且新的文件进入它将读取新的文件,而不提示我。
它给我的错误是强制为unicode:需要字符串或缓冲区,找到列表。
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.
推荐答案
os.stat
会告诉你修改时间
os.stat
will tell you the modification time
这篇关于文件修改和创建的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!