Search code examples
pythonstat

How to re-instatiate stavfs output


Why does the last statement fails?

>>> import posix
>>> os.statvfs('/boot')
posix.statvfs_result(f_bsize=1024, f_frsize=1024, f_blocks=495844L, f_bfree=412223L, f_bavail=386623L, f_files=128016L, f_ffree=127969L, f_favail=127969L, f_flag=4096, f_namemax=255)
>>> posix.statvfs_result(f_bsize=1024, f_frsize=1024, f_blocks=495844L, f_bfree=412223L, f_bavail=386623L, f_files=128016L, f_ffree=127969L, f_favail=127969L, f_flag=4096, f_namemax=255)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
TypeError: structseq() takes at most 2 arguments (10 given)

The "system" asks that I explain why my problem is different than some potential duplicate. It is not so much that my problem is that different, it's just that I find that providing a direct solution here is better for pragmatic reasons: the answer to the other question doesn't clearly establish what is happening and, in doing so, keeps the scope of the answer narrow and more difficult to generalize.


Solution

  • Thanks to the duplicate suggestion I was able to transpose its solution to my problem.

    Changing

    posix.statvfs_result(f_bsize=1024, f_frsize=1024, f_blocks=495844L, f_bfree=412223L, f_bavail=386623L, f_files=128016L, f_ffree=127969L, f_favail=127969L, f_flag=4096, f_namemax=255)
    

    to

    posix.statvfs_result((1024, 1024, 495844L, 412223L, 386623L, 128016L, 127969L, 127969L, 4096, 255))
    

    solves my problem:

    >>> posix.statvfs_result((1024, 1024, 495844L, 412223L, 386623L, 128016L, 127969L, 127969L, 4096, 255))
    posix.statvfs_result(f_bsize=1024, f_frsize=1024, f_blocks=495844L, f_bfree=412223L, f_bavail=386623L, f_files=128016L, f_ffree=127969L, f_favail=127969L, f_flag=4096, f_namemax=255)
    

    So to make this more complete and convenient, here is how converting to tuple allows recreating the results of os.statvfs:

    >>> tuple(os.statvfs('/boot'))
    (1024, 1024, 495844L, 412223L, 386623L, 128016L, 127969L, 127969L, 4096, 255)
    >>> posix.statvfs_result(tuple(os.statvfs('/boot')))
    posix.statvfs_result(f_bsize=1024, f_frsize=1024, f_blocks=495844L, f_bfree=412223L, f_bavail=386623L, f_files=128016L, f_ffree=127969L, f_favail=127969L, f_flag=4096, f_namemax=255)