python os.stat 返回始终相同的 mtime/size

发布于 2024-10-09 19:32:17 字数 2837 浏览 6 评论 0原文

使用以下代码,我总是得到相同的大小和 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

Saygoodbye 2024-10-16 19:32:17

将 os.stat(path) 更改为 os.stat(complete_path) ,它会重新统计每个文件传入的相同路径。

您可以使用 将该纪元时间戳记放入 time 对象中time.localtime()(或time.gmtime()

Change os.stat(path) to os.stat(complete_path), it's re-stating the same path passed in for every file.

And you can get that epoch timestamp into a time object using time.localtime() (or time.gmtime())

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文