我编写了以下程序,列出了不同文件夹中的最后文件创建时间:
import os, os.path, time
path = raw_input("BackUp root Path: ")
folders = os.listdir(path)
report_list = dict()
for x in folders:
if os.path.isfile(path+"\\"+x):
folders.remove(x)
print
print
print "Last file creation time:"
for folder in folders:
for root,dirs,files in os.walk(path+"\\"+folder):
for filename in files:
if not report_list.has_key(folder):
report_list[folder]=time.strftime('%Y/%m/%d', time.gmtime(os.path.getctime(root+"\\"+filename)))
this_file_date = time.strftime('%Y/%m/%d', time.gmtime(os.path.getctime(root+"\\"+filename)))
if int(report_list[folder].translate(None,"/"))< int(this_file_date.translate(None,"/")):
report_list[folder]= this_file_date
for folder in folders:
if report_list.has_key(folder):
print " {:<25}--->{:>15}".format(folder,report_list[folder])
else:
print " {:<25}--->{:>15}".format(folder,"Empty!")
print
print
my_filter = raw_input("Do you want to filter output? (y/n): ")
if my_filter== 'y':
date=raw_input("Enter date(Example --> 2010/10/23): ")
print
print
print "Filtered output:"
for folder in folders:
if report_list.has_key(folder):
if int(report_list[folder].translate(None,"/"))< int(date.translate(None,"/")):
print " {:<25}--->{:>15}".format(folder,report_list[folder])
else:
print " {:<25}--->{:>15}".format(folder,"Empty!")
嗯,它有时会返回错误。例如,当我将地址//10.0.0.100/shares
作为输入时,我面临以下错误,表明文件不存在!
虽然文件确实在那里:
(无论如何,搜索它,意味着当我创建列表时它就在那里!)
答案 0 :(得分:0)
问题显然是因为文件名使用波斯语字符。将您的文件名更改为英文字符,它可以工作。 而且,而不是
x+"\\"+y
使用:
os.path.join(x,y)
答案 1 :(得分:0)
我用 Python 3.5 重写程序,现在它工作正常,没有任何错误:
import os, os.path, time
path = input("BackUp root Path: ")
folders = os.listdir(path)
report_list = dict()
for x in folders:
if os.path.isfile(path+"\\"+x):
folders.remove(x)
print ()
print ()
print ("Last file creation time:")
for folder in folders:
for root,dirs,files in os.walk(path+"\\"+folder):
for filename in files:
if not folder in report_list:
report_list[folder]=time.strftime('%Y/%m/%d', time.gmtime(os.path.getctime(root+"\\"+filename)))
this_file_date = time.strftime('%Y/%m/%d', time.gmtime(os.path.getctime(root+"\\"+filename)))
if int(report_list[folder].replace("/",""))< int(this_file_date.replace("/","")):
report_list[folder]= this_file_date
if folder in report_list:
print(" {:<25}{:>15}".format(folder,report_list[folder]))
else:
print(" {:<25}{:>15}".format(folder,"Empty!"))
print
print
my_filter = input("Do you want to filter output? (y/n): ")
if my_filter== 'y':
date=input("Enter date(Example --> 2010/10/23): ")
print ()
print ()
print ("Filtered output:")
for folder in folders:
if folder in report_list:
if int(report_list[folder].replace("/",""))< int(date.replace("/","")):
print(" {:<25}{:>15}".format(folder,report_list[folder]))
else:
print(" {:<25}{:>15}".format(folder,"Empty!"))