def _maybepossible(d,allowfixed=0): drive=chr(ord('A')+d) # here if the drive is at least there drname='%c:\\' % drive[0] t=win32file.GetDriveType(drname) if t == win32file.DRIVE_REMOVABLE: try: fr,tot,totfr=win32file.GetDiskFreeSpaceEx(drname) if tot > 30000000 and tot < 66000000000: return 1 except: return 0 else: if 1: if t == win32file.DRIVE_FIXED or t == win32file.DRIVE_REMOVABLE: # see if it is already a remote drive ans,lns=_checksdhdr(drive) if ans == 0: return 1 # must determine size, etc. try: fr,tot,totfr=win32file.GetDiskFreeSpaceEx(drname) except: tot=0 if tot > 30000000 and tot < 66000000000: if _regcheck(drive) or allowfixed: return 1 return 0
def get_free_space(path): try: path = os.path.dirname(path) return win32file.GetDiskFreeSpaceEx(path)[0] except OSError: traceback.print_exc() return
def get_available_space_in_bytes(path): r"""Return the number of bytes available in the given path. The meaning of 'available' varies between host platforms but the intention is to return the writable space available to this process (i.e. implentations try to take user quotas or filesystem restrictions into account). The space is not reserved so this value can only be used as a hint - it is not a guarantee that the space will be available for consumption at a later point. :param string path: a string or bytes object giving the pathname of the path to be checked. :return: if successful, a non-zero value representing the number of bytes available; if the function fails, the return value is zero (0). :rtype: int Example: >>> get_available_space_in_bytes("c:\\") # doctest: +SKIP 1435425335 """ return win32file.GetDiskFreeSpaceEx(path)[0]
def main(argv): #------------------------------------------------------------------------------- """ usage: drives.py shows the available drives on the system and (free space/total space) """ args, opts = oss.gopt(argv[1:], [], [], main.__doc__) drives = w32.GetLogicalDrives() for i in range(26): if drives & (1 << i): dl = chr(i + ord('A')) rootpath = dl + ':\\' tp = w32.GetDriveType(rootpath) print(" %s:" % dl, S[tp], end='') try: f, t, d = w32.GetDiskFreeSpaceEx(rootpath) if tp == 4: print(" (%s/%s)" % (util.CvtGigMegKBytes(f), util.CvtGigMegKBytes(t)), end='') print(" [%s]" % wnet.WNetGetUniversalName(rootpath, 1)) else: print(" (%s/%s)" % (util.CvtGigMegKBytes(f), util.CvtGigMegKBytes(t))) except: print(" -- not ready") oss.exit(0)
def free_space(pathname): """Return free space in bytes""" if 'nt' == os.name: _, _, free_bytes = win32file.GetDiskFreeSpaceEx(pathname) return free_bytes mystat = os.statvfs(pathname) return mystat.f_bfree * mystat.f_bsize
def get_free_space(disk): free_space = 0 try: free_space_tuple = win32file.GetDiskFreeSpaceEx(disk) free_space = free_space_tuple[0] except Exception, e: pass # ignores failures for inappropriate disks
def getfreespace(path): while True: try: return win32file.GetDiskFreeSpaceEx(path)[0] except: path = os.path.split(path)[0] if not path: raise
def print_disk_size(self): for disk in self.disks: free_bytes, total_bytes, total_free_bytes = win32file.GetDiskFreeSpaceEx( disk) # print("%s %s %s" % tuple(map(convert_size, self.get_disk_size(disk)))) print("%s %s %s %.2f%%" % (disk, convert_size(free_bytes), convert_size(total_bytes), (total_bytes - free_bytes) * 100 / total_bytes))
def get_free_space(path): try: path = os.path.dirname(path) while path: if os.path.exists(path): return win32file.GetDiskFreeSpaceEx(path)[0] path = os.path.split(path)[0] except OSError: traceback.print_exc() return
def driveFreeSpace(): """Retrieves the cwd's filesystem stats""" if os.name == 'posix': # get free space if they have statvfs data = os.statvfs(consts.cwd) return convert_file_size(float(data[0] * data[4])) if os.name == 'nt': drive = os.path.splitdrive(consts.cwd)[0] # Gets drive letter d_size = win32file.GetDiskFreeSpaceEx(drive) return convert_file_size(d_size[2]) # unsupported OS, return error return 'Unavailable'
def is_UDisk(drives): UDisk = [] for item in drives: try: free_bytes, total_bytes, total_free_bytes = win32file.GetDiskFreeSpaceEx( item) if (total_bytes / 1024 / 1024 / 1024) < 17: UDisk.append(item) except: break return UDisk
def getfreespace(path): # Boudewijn: the win32file module is NOT unicode # safe! We will try directories further up the # directory tree in the hopes of getting a path on # the same disk without the unicode... while True: try: return win32file.GetDiskFreeSpaceEx(path)[0] except: path = os.path.split(path)[0] if not path: raise
def drive_is_ok(letter, max_tries=10, debug=False): import win32file with drive_ok_lock: for i in range(max_tries): try: win32file.GetDiskFreeSpaceEx(letter + ':\\') return True except Exception as e: if i >= max_tries - 1 and debug: prints('Unable to get free space for drive:', letter) prints(as_unicode(e)) time.sleep(0.2) return False
def test_drive(drive: str): """ Tests if drive is ready using Windows API :param drive: Drive from which backup is meant to be performed :return: True if device is ready, False otherwise """ try: win32file.GetDiskFreeSpaceEx(drive) except win32api.error as err: if 'The device is not ready.' not in err.args: logging.error(err) raise err return False return True
def free_space(pathname): """Return free space in bytes""" if 'nt' == os.name: from bleachbit import Windows if Windows.parse_windows_build() >= 6: # This works better with UTF-8 paths. import psutil return psutil.disk_usage(pathname).free else: # This works better with Windows XP but not UTF-8. # Deprecated. _fb, _tb, total_free_bytes = win32file.GetDiskFreeSpaceEx(pathname) return total_free_bytes mystat = os.statvfs(pathname) return mystat.f_bfree * mystat.f_bsize
def free_space(target_dir, log): try: if sys.platform == 'win32': import win32file free_bytes, total_bytes, total_free_bytes = win32file.GetDiskFreeSpaceEx( target_dir) else: import os, statvfs s = os.statvfs(target_dir) total_free_bytes = s[statvfs.F_BSIZE] * s[statvfs.F_BAVAIL] except Exception, e: if log: log.error('checking directory "%s": %s', target_dir, e) raise e
def drive_is_ok(self, letter, debug=False): import win32api, win32file with self.lock: oldError = win32api.SetErrorMode(1) #SEM_FAILCRITICALERRORS = 1 try: ans = True try: win32file.GetDiskFreeSpaceEx(letter+':\\') except Exception as e: if debug: prints('Unable to get free space for drive:', letter) prints(as_unicode(e)) ans = False return ans finally: win32api.SetErrorMode(oldError)
def drive_is_ok(letter, max_tries=10, debug=False): import win32api, win32file with drive_ok_lock: oldError = win32api.SetErrorMode(1) # SEM_FAILCRITICALERRORS = 1 try: for i in range(max_tries): try: win32file.GetDiskFreeSpaceEx(letter+':\\') return True except Exception as e: if i >= max_tries - 1 and debug: prints('Unable to get free space for drive:', letter) prints(as_unicode(e)) time.sleep(0.2) return False finally: win32api.SetErrorMode(oldError)
def get_free_disk_space_win32(path): """ Win32-specific code to determine the free disk space remaining for a given path. Uses code from: http://mail.python.org/pipermail/python-list/2003-May/203223.html """ drive, tail = os.path.splitdrive(path) try: import win32file userFree, userTotal, freeOnDisk = win32file.GetDiskFreeSpaceEx(drive) return userFree except ImportError: logger.warn('Running on Win32 but win32api/win32file not installed.') # Cannot determine free disk space return 0
current_drive = current_drive + "\\" # Check if "isCrypted" exists isCryptedFile = current_drive + "\\" + "isCrypted" exists = os.path.isfile(isCryptedFile) print(exists) if not exists: allFiles = getListOfFiles(current_drive) # Remove current file from allFiles try: allFiles.remove(__file__) except ValueError: pass # Get total and free space in current drive total = win32file.GetDiskFreeSpaceEx(current_drive)[1] free = win32file.GetDiskFreeSpaceEx(current_drive)[2] # Get unique System UUID command = (resource_path('dmidecode.exe') + ' -s system-uuid').split() cpuID = subprocess.check_output( command, **subprocess_args(False)).decode('utf-8') # Load salt and hash the unique UUID salt = load_object(resource_path("salt")) keyToCrypt = hashStr(cpuID, salt) # marquer le disque comme crypté with open(isCryptedFile, "w") as file: file.write(cpuID)
def catalog_drive_movies(directory, files, drive_name): cataloged = [] problems = [] #Delete all records for this drive Movie_Drive.objects.filter(drive=drive_name).delete() #Save the drive data drive_info = win32file.GetDiskFreeSpaceEx(directory) drive = Movie_Drive.objects.create(drive=drive_name, drive_capacity=int(drive_info[1]), drive_free_space=int(drive_info[0])) for file_object in files: if file_object.lower() not in IGNORE_FILES and file_object != "0": path = directory + str(file_object) movie_name = "" if os.path.isdir(path) != True: file = str(file_object).split('-') if file.__len__() > 1: movie_name = str(file[0]) else: movie_name = str(files).rsplit('.', 1)[0] file_size = str(os.path.getsize(file_object)) file_name = str(file_object) else: try: file_list = os.listdir(path) for files in file_list: if 'HD.Movies' in drive_name: if ".mkv" in files or '.MKV' in files: file = str(files).split('-') if file.__len__() > 1: movie_name = str(file[0]) else: movie_name = str(files).rsplit('.', 1)[0] file_size = str( os.path.getsize(path + '/' + files)) file_name = str(files) break elif 'Movies' in drive_name: if ".avi" in files or '.AVI' in files: file = str(files).split('-') if file.__len__() > 1: movie_name = str(file[0]) else: movie_name = str(files).rsplit('.', 1)[0] file_size = str( os.path.getsize(path + '/' + files)) file_name = str(files) break except: pass if movie_name != "": Movie_Name.objects.create(drive=drive, movie_name=movie_name.replace( '.', ' '), file_name=file_name, file_size=file_size) cataloged.append(str(file_object)) else: problems.append(str(file_object)) return cataloged, problems
warn = "!" if server != lastserv and uid != '': try: win32wnet.WNetAddConnection2(win32netcon.RESOURCETYPE_DISK, None, server, None, uid, pw, 0) except: print "%s\t (WNetAddConnection2 failed [%s])" % (server, uid) lastserv = server fs, ts, fp = 0, 0, 0 if uid != '': try: space = win32file.GetDiskFreeSpaceEx(share) except: warn = " " fp = "(GetDiskFreeSpaceEx failed)" fs, ts = '', '' else: fs = int(space[0] / 1048576) ts = int(space[1] / 1048576) fp = int((float(space[0]) / float(space[1])) * 100) if fp < 16: warn = "!" else: warn = " " print "%s \t %s%s \t %s\t\t%s" % (share[2:], warn, fp, fs, ts) try: win32wnet.WNetCancelConnection2(lastserv, 0, 0)
def get_disk_size(self, path): return win32file.GetDiskFreeSpaceEx(path)
def freespace(drive): try: fr,tot,totfr=win32file.GetDiskFreeSpaceEx(drive) return fr except: return None
os.SEEK_END = 2 try: from os import statvfs import statvfs def getfreespace(path): s = os.statvfs(path.encode('utf-8')) size = s[statvfs.F_BAVAIL] * long(s[statvfs.F_BSIZE]) return size except: if sys.platform == 'win32': try: import win32file try: win32file.GetDiskFreeSpaceEx('.') def getfreespace(path): while True: try: return win32file.GetDiskFreeSpaceEx(path)[0] except: path = os.path.split(path)[0] if not path: raise except: def getfreespace(path): spc, bps, nfc, tnc = win32file.GetDiskFreeSpace(path) return long(nfc) * long(spc) * long(bps)
def getfreespace(path): list = win32file.GetDiskFreeSpaceEx(path) return list[0]
def getfreespace(path): s = os.statvfs(path) size = s[statvfs.F_BAVAIL] * long(s[statvfs.F_BSIZE]) return size except: if (sys.platform == 'win32'): try: # Windows if win32all extensions are installed import win32file try: # Win95 OSR2 and up # Arno: this code was totally broken as the method returns # a list of values indicating 1. free space for the user, # 2. total space for the user and 3. total free space, so # not a single value. test = win32file.GetDiskFreeSpaceEx(".") def getfreespace(path): list = win32file.GetDiskFreeSpaceEx(path) return list[0] except: # Original Win95 # (2GB limit on partition size, so this should be # accurate except for mapped network drives) # Arno: see http://aspn.activestate.com/ASPN/docs/ActivePython/2.4/pywin32/win32file__GetDiskFreeSpace_meth.html def getfreespace(path): [spc, bps, nfc, tnc] = win32file.GetDiskFreeSpace(path) return long(nfc) * long(spc) * long(bps) except ImportError: # Windows if win32all extensions aren't installed # (parse the output from the dir command)