本文整理汇总了Python中win32file.SetFilePointer方法的典型用法代码示例。如果您正苦于以下问题:Python win32file.SetFilePointer方法的具体用法?Python win32file.SetFilePointer怎么用?Python win32file.SetFilePointer使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类win32file
的用法示例。
在下文中一共展示了win32file.SetFilePointer方法的10个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: ZapMBRGPT
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import SetFilePointer [as 别名]
def ZapMBRGPT(self, disk_size, sector_size, add1MB):
self.assert_physical_drive()
# Implementation borrowed from rufus: https://github.com/pbatard/rufus
num_sectors_to_clear \
= (add1MB and 2048 or 0) + self.MAX_SECTORS_TO_CLEAR
zeroBuf = b'\0' * sector_size
for i in range(num_sectors_to_clear):
self.WriteFile(zeroBuf)
offset = disk_size - self.MAX_SECTORS_TO_CLEAR * sector_size
win32file.SetFilePointer(self.h, offset, win32con.FILE_BEGIN)
for i in range(num_sectors_to_clear):
self.WriteFile(zeroBuf)
# We need to append paddings as CREATE_DISK structure contains a union.
param = struct.pack('<IIIHH8s',
winioctlcon.PARTITION_STYLE_MBR, 0xdeadbeef,
0,0,0,b'abcdefgh')
win32file.DeviceIoControl(
self.h, winioctlcon.IOCTL_DISK_CREATE_DISK, param, 0, None)
示例2: DumpWithRead
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import SetFilePointer [as 别名]
def DumpWithRead(self, output_filename):
"""Read the image and write all the data to a raw file."""
with open(output_filename, "wb") as outfd:
offset = 0
for start, length in self.runs:
if start > offset:
print("\nPadding from 0x%X to 0x%X\n" % (offset, start))
self.PadWithNulls(outfd, start - offset)
offset = start
end = start + length
while offset < end:
to_read = min(self.buffer_size, end - offset)
win32file.SetFilePointer(self.fd, offset, 0)
_, data = win32file.ReadFile(self.fd, to_read)
outfd.write(data)
offset += to_read
offset_in_mb = offset/1024/1024
if not offset_in_mb % 50:
sys.stdout.write("\n%04dMB\t" % offset_in_mb)
sys.stdout.write(".")
sys.stdout.flush()
示例3: read
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import SetFilePointer [as 别名]
def read(self, offset, length):
try:
win32file.SetFilePointer(self.fhandle, offset, 0)
_, data = win32file.ReadFile(self.fhandle, length)
except Exception:
return addrspace.ZEROER.GetZeros(length)
return data
示例4: write
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import SetFilePointer [as 别名]
def write(self, offset, data):
win32file.SetFilePointer(self.fhandle, offset, 0)
# The WinPmem driver returns bytes_written == 0 always. This is probably
# a bug in its write routine, so we ignore it here. If the operation was
# successful we assume all bytes were written.
err, _bytes_written = win32file.WriteFile(self.fhandle, data)
if err == 0:
return len(data)
return 0
示例5: testMoreFiles
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import SetFilePointer [as 别名]
def testMoreFiles(self):
# Create a file in the %TEMP% directory.
testName = os.path.join( win32api.GetTempPath(), "win32filetest.dat" )
desiredAccess = win32file.GENERIC_READ | win32file.GENERIC_WRITE
# Set a flag to delete the file automatically when it is closed.
fileFlags = win32file.FILE_FLAG_DELETE_ON_CLOSE
h = win32file.CreateFile( testName, desiredAccess, win32file.FILE_SHARE_READ, None, win32file.CREATE_ALWAYS, fileFlags, 0)
# Write a known number of bytes to the file.
data = str2bytes("z") * 1025
win32file.WriteFile(h, data)
self.failUnless(win32file.GetFileSize(h) == len(data), "WARNING: Written file does not have the same size as the length of the data in it!")
# Ensure we can read the data back.
win32file.SetFilePointer(h, 0, win32file.FILE_BEGIN)
hr, read_data = win32file.ReadFile(h, len(data)+10) # + 10 to get anything extra
self.failUnless(hr==0, "Readfile returned %d" % hr)
self.failUnless(read_data == data, "Read data is not what we wrote!")
# Now truncate the file at 1/2 its existing size.
newSize = len(data)//2
win32file.SetFilePointer(h, newSize, win32file.FILE_BEGIN)
win32file.SetEndOfFile(h)
self.failUnlessEqual(win32file.GetFileSize(h), newSize)
# GetFileAttributesEx/GetFileAttributesExW tests.
self.failUnlessEqual(win32file.GetFileAttributesEx(testName), win32file.GetFileAttributesExW(testName))
attr, ct, at, wt, size = win32file.GetFileAttributesEx(testName)
self.failUnless(size==newSize,
"Expected GetFileAttributesEx to return the same size as GetFileSize()")
self.failUnless(attr==win32file.GetFileAttributes(testName),
"Expected GetFileAttributesEx to return the same attributes as GetFileAttributes")
h = None # Close the file by removing the last reference to the handle!
self.failUnless(not os.path.isfile(testName), "After closing the file, it still exists!")
示例6: testFilePointer
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import SetFilePointer [as 别名]
def testFilePointer(self):
# via [ 979270 ] SetFilePointer fails with negative offset
# Create a file in the %TEMP% directory.
filename = os.path.join( win32api.GetTempPath(), "win32filetest.dat" )
f = win32file.CreateFile(filename,
win32file.GENERIC_READ|win32file.GENERIC_WRITE,
0,
None,
win32file.CREATE_ALWAYS,
win32file.FILE_ATTRIBUTE_NORMAL,
0)
try:
#Write some data
data = str2bytes('Some data')
(res, written) = win32file.WriteFile(f, data)
self.failIf(res)
self.assertEqual(written, len(data))
#Move at the beginning and read the data
win32file.SetFilePointer(f, 0, win32file.FILE_BEGIN)
(res, s) = win32file.ReadFile(f, len(data))
self.failIf(res)
self.assertEqual(s, data)
#Move at the end and read the data
win32file.SetFilePointer(f, -len(data), win32file.FILE_END)
(res, s) = win32file.ReadFile(f, len(data))
self.failIf(res)
self.failUnlessEqual(s, data)
finally:
f.Close()
os.unlink(filename)
示例7: fseek
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import SetFilePointer [as 别名]
def fseek(self, handle, pos, how=0):
"Seek in the file object."
fo = self._files.get(handle, None)
if fo:
if type(fo) is WindowsFile:
return fo.seek(pos, how)
else:
win32file.SetFilePointer(fo, pos, how)
示例8: DumpWithRead
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import SetFilePointer [as 别名]
def DumpWithRead(self, output_filename):
"""Read the image and write all the data to a raw file."""
with open(output_filename, "wb") as outfd:
offset = 0
for start, length in self.runs:
if start > offset:
print "\nPadding from 0x%X to 0x%X\n" % (offset, start)
self.PadWithNulls(outfd, start - offset)
offset = start
end = start + length
while offset < end:
to_read = min(self.buffer_size, end - offset)
win32file.SetFilePointer(self.fd, offset, 0)
_, data = win32file.ReadFile(self.fd, to_read)
outfd.write(data)
offset += to_read
offset_in_mb = offset / 1024 / 1024
if not offset_in_mb % 50:
sys.stdout.write("\n%04dMB\t" % offset_in_mb)
sys.stdout.write(".")
sys.stdout.flush()
示例9: _sparse_magic
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import SetFilePointer [as 别名]
def _sparse_magic(handle, length=0):
win32file.DeviceIoControl(handle, FSCTL_SET_SPARSE, '', 0, None)
win32file.SetFilePointer(handle, length, win32file.FILE_BEGIN)
win32file.SetEndOfFile(handle)
win32file.SetFilePointer(handle, 0, win32file.FILE_BEGIN)
示例10: _read
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import SetFilePointer [as 别名]
def _read(self, addr, length, pad = False):
offset = self.translate(addr)
if offset == None:
if pad:
return "\x00" * length
else:
return None
win32file.SetFilePointer(self.fhandle, offset, 0)
data = win32file.ReadFile(self.fhandle, length)[1]
return data