[issue410547] os.statvfs support for Windows

2022-04-10 Thread admin


Change by admin :


--
github: None -> 34217

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
https://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue410547] os.statvfs support for Windows

2011-01-09 Thread David-Sarah Hopwood

David-Sarah Hopwood  added the comment:

Don't use win32file.GetDiskFreeSpace; the underlying Windows API only supports 
drives up to 2 GB 
(http://blogs.msdn.com/b/oldnewthing/archive/2007/11/01/5807020.aspx). Use 
GetFreeDiskSpaceEx, as the code I linked to does.

I'm not sure it makes sense to provide an exact clone of os.statvfs, since some 
of the statvfs fields don't have equivalents that are obtainable by any Windows 
API as far as I know. What would make sense is a cross-platform way to 
get total disk space, and the space free for root/Administrator and for the 
current user. This would actually be somewhat easier to use on Unix as well.

Anyway, here's some code for Windows that only uses ctypes (whichdir should be 
Unicode):

from ctypes import WINFUNCTYPE, windll, POINTER, byref, c_ulonglong
from ctypes.wintypes import BOOL, DWORD, LPCWSTR

# 
PULARGE_INTEGER = POINTER(c_ulonglong)

# 
GetDiskFreeSpaceExW = WINFUNCTYPE(BOOL, LPCWSTR, PULARGE_INTEGER, 
PULARGE_INTEGER, PULARGE_INTEGER)(
("GetDiskFreeSpaceExW", windll.kernel32))

# 
GetLastError = WINFUNCTYPE(DWORD)(("GetLastError", windll.kernel32))

# (This might put up an error dialog unless
# SetErrorMode(SEM_FAILCRITICALERRORS | SEM_NOOPENFILEERRORBOX)
# has been called.)

n_free_for_user = c_ulonglong(0)
n_total = c_ulonglong(0)
n_free  = c_ulonglong(0)
retval = GetDiskFreeSpaceExW(whichdir,
 byref(n_free_for_user),
 byref(n_total),
 byref(n_free))
if retval == 0:
raise OSError("Windows error %d attempting to get disk statistics for 
%r"
  % (GetLastError(), whichdir))

free_for_user = n_free_for_user.value
total = n_total.value
free  = n_free.value

--
versions: +Python 2.7, Python 3.3 -Python 2.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue410547] os.statvfs support for Windows

2011-01-09 Thread Carsten Koch

Carsten Koch  added the comment:

Here is what I am doing now:

...
if sys.platform == "win32":
   import win32file
else:
   import statvfs
...


def get_free_space(path = '.'):
   """
  Determine the free space in bytes for the given path.
   """
   if sys.platform == "win32":
  sizes = win32file.GetDiskFreeSpace(path)
  return sizes[0] * sizes[1] * sizes[2]
   else:
  status = os.statvfs(path)
  return status[statvfs.F_BAVAIL] * status[statvfs.F_FRSIZE]



def get_partition_size(path = '.'):
   """
  Determine the total space in bytes for the given path.
   """
   if sys.platform == "win32":
  sizes = win32file.GetDiskFreeSpace(path)
  return sizes[0] * sizes[1] * sizes[3]
   else:
  status = os.statvfs(path)
  return status[statvfs.F_BLOCKS] * status[statvfs.F_FRSIZE]

--
versions: +Python 2.6

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com



[issue410547] os.statvfs support for Windows

2011-01-06 Thread David-Sarah Hopwood

David-Sarah Hopwood  added the comment:

"Is there a portable way to get the available disk space by now?"

No, but 
http://tahoe-lafs.org/trac/tahoe-lafs/browser/src/allmydata/util/fileutil.py?rev=4894#L308
 might be helpful (uses pywin32).

--
nosy: +davidsarah

___
Python tracker 

___
___
Python-bugs-list mailing list
Unsubscribe: 
http://mail.python.org/mailman/options/python-bugs-list/archive%40mail-archive.com