使用以下代码,我总是得到相同的大小和mtime:
# -*- coding: utf-8 -*-
import gtk
import pygtk
import os
class LocalFileTreeView(gtk.TreeView):
def __init__(self, model = None):
super(LocalFileTreeView, self).__init__(model)
self.model = gtk.TreeStore(gtk.gdk.Pixbuf, str, str, str)
self.create_columns()
self.set_model_data('/home/foo/Downloads/')
self.set_model(self.model)
def create_columns(self):
self.icon_cell_renderer = gtk.CellRendererPixbuf()
self.filename_cell_renderer = gtk.CellRendererText()
self.size_cell_renderer = gtk.CellRendererText()
self.last_modified_cell_renderer = gtk.CellRendererText()
self.icon_column = gtk.TreeViewColumn(None, self.icon_cell_renderer, pixbuf = 0)
self.filename_column = gtk.TreeViewColumn('filename', self.filename_cell_renderer, text = 1)
self.size_column = gtk.TreeViewColumn('size', self.size_cell_renderer, text = 2)
self.last_modified_column = gtk.TreeViewColumn('last modified', self.last_modified_cell_renderer, text = 3)
self.append_column(self.icon_column)
self.append_column(self.filename_column)
self.append_column(self.size_column)
self.append_column(self.last_modified_column)
def set_model_data(self, path, iter = None):
try:
local_files = os.listdir(path)
local_files.sort()
except OSError:
local_files = []
for local_file in local_files:
complete_path = path + local_file
stat = os.stat(path)
if os.path.isdir(complete_path):
dir_iter = self.model.append(iter, [self.render_icon(gtk.STOCK_DIRECTORY, gtk.ICON_SIZE_SMALL_TOOLBAR), local_file, None, str(stat.st_mtime)])
self.set_model_data(complete_path + '/', dir_iter)
else:
self.model.append(iter, [self.render_icon(gtk.STOCK_FILE, gtk.ICON_SIZE_SMALL_TOOLBAR), local_file, str(stat.st_size), str(stat.st_mtime)])
class LocalFileTreeViewTest:
def __init__(self):
self.window = gtk.Window()
self.local_file_treeview = LocalFileTreeView()
self.scrolled_window = gtk.ScrolledWindow()
self.window.set_title('LocalFileTreeView Test')
self.window.connect('destroy', gtk.main_quit)
self.scrolled_window.set_policy(gtk.POLICY_AUTOMATIC, gtk.POLICY_AUTOMATIC)
self.scrolled_window.add(self.local_file_treeview)
self.window.add(self.scrolled_window)
self.local_file_treeview.show()
self.scrolled_window.show()
self.window.show()
if __name__ == '__main__':
local_file_treeview_test = LocalFileTreeViewTest()
gtk.main()
我总是得到4096作为大小和1293898432.0作为mtime(我如何格式化?) 我怎样才能得到正确的价值观?
谢谢。
答案 0 :(得分:6)
将os.stat(path)
更改为os.stat(complete_path)
,重新stat
传入每个文件的相同路径。
您可以使用time.localtime()
(或time
)
time.gmtime()
对象中