python os.stat 返回始终相同的 mtime/size
使用以下代码,我总是得到相同的大小和 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 (我如何格式化它?) 我怎样才能得到正确的价值观?
谢谢。
With the following code I always get the same size and 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()
I always get 4096 as size and 1293898432.0 as mtime (how do I format that?)
How could I get the right values?
Thank you.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
将 os.stat(path) 更改为 os.stat(complete_path) ,它会重新统计每个文件传入的相同路径。
您可以使用 将该纪元时间戳记放入
time
对象中time.localtime()
(或time.gmtime()
)Change
os.stat(path)
toos.stat(complete_path)
, it's re-stat
ing the same path passed in for every file.And you can get that epoch timestamp into a
time
object usingtime.localtime()
(ortime.gmtime()
)