【问题标题】:os.stat() on WindowsWindows 上的 os.stat()
【发布时间】:2011-03-11 16:43:24
【问题描述】:

os.stat() 中的哪些字段在 Windows 上用虚拟值填充?

python 文档对此并不清楚。特别是,st_ino 在 Windows 上产生了什么?

有人可以在 Windows 上运行交互式 python 会话并告诉我吗?我没有 Windows 机器,所以我做不到。

【问题讨论】:

    标签: python windows


    【解决方案1】:

    st_inost_devst_nlinkst_uidst_gid 是 Windows 7 SP1 到 Python 2.7.11 上的虚拟变量:

    import os; os.stat('Desktop\test.txt')
    nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0L, st_nlink=0, st_uid=0, st_gid=0, st_size=293L, st_atime=1448376581L, st_mtime=1451782006L, st_ctime=1448376581L)
    

    但是,从 Python 3.5.1 开始,它们似乎在 Windows 7 SP1 中填充了有意义的值:

    import os; os.stat('Desktop\test.txt')
    os.stat_result(st_mode=33206, st_ino=17732923532870243, st_dev=2289627604, st_nlink=2, st_uid=0, st_gid=0, st_size=293, st_atime=1448376581, st_mtime=1451782006, st_ctime=1448376581)
    

    有关此主题的 Python 文档将引导理智的用户避免在 Windows 中使用 os.stat,因为无法保证 any 字段将始终/永远准确。在实践中,st_sizest_atimest_mtimest_ctime 看起来通常(如果不总是准确的话)是准确的。其他字段至少取决于 Python 版本,可能还取决于 Windows 版本,可能还有其他因素。

    【讨论】:

      【解决方案2】:

      这是一个测试运行:

      C:\WINDOWS>echo test > test.txt
      
      C:\WINDOWS>python
      Python 2.6.5 (r265:79096, Mar 19 2010, 21:48:26) [MSC v.1500 32 bit (Intel)] on
      win32
      Type "help", "copyright", "credits" or "license" for more information.
      >>> import os
      >>> os.stat('test.txt')
      nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=
      0, st_size=7L, st_atime=1299861919L, st_mtime=1299861919L, st_ctime=1299861919L)
      
      >>>
      

      【讨论】:

        【解决方案3】:

        在 Python 3.3.4 中

        >>> os.stat('.')
        nt.stat_result(st_mode=16895, st_ino=1407374883604316, st_dev=0, st_nlink=1, st_uid=0,
        st_gid=0, st_size=4096, st_atime=1392476826, st_mtime=1392476826, st_ctime=1392374365)
        

        实现了与旧版本不同的st_ino

        【讨论】:

        • 从 3.4.0 开始,实现了 st_dev
        【解决方案4】:

        Python 3.1.2 说:

        >>> os.stat("C:\\autoexec.bat")
        nt.stat_result(st_mode=33279, st_ino=0, st_dev=0, st_nlink=0, st_uid=0, st_gid=0,
        st_size=0, st_atime=1150614982, st_mtime=1150614982, st_ctime=1150614982)
        

        【讨论】:

          【解决方案5】:

          Python 3:

          >>> os.stat( r'C:\Users\poke\Desktop\test.txt' )
          nt.stat_result(st_mode=33206, st_ino=0, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=252, st_atime=1299861949, st_mtime=1298245084, st_ctime=1299861949)
          

          你还需要什么吗?

          【讨论】:

            【解决方案6】:

            我在 python 3.4 中运行了os.stat

            这是我使用的代码

            import os
            
            
            myPath = os.path.expanduser("~")
            os.chdir(myPath)
            
            files = os.listdir()
            
            for file in files:
                info = os.stat(file)
                print ("{0:>20} {1:>8}".format(file, info.st_size))
            

            【讨论】:

            • 没有输出,这没什么用。
            猜你喜欢
            • 1970-01-01
            • 1970-01-01
            • 1970-01-01
            • 2020-07-15
            • 2013-09-28
            • 1970-01-01
            • 2011-12-24
            • 2019-06-05
            • 2017-07-09
            相关资源
            最近更新 更多