本文整理汇总了Python中win32file.CreateFile方法的典型用法代码示例。如果您正苦于以下问题:Python win32file.CreateFile方法的具体用法?Python win32file.CreateFile怎么用?Python win32file.CreateFile使用的例子?那么恭喜您, 这里精选的方法代码示例或许可以为您提供帮助。您也可以进一步了解该方法所在类win32file
的用法示例。
在下文中一共展示了win32file.CreateFile方法的15个代码示例,这些例子默认根据受欢迎程度排序。您可以为喜欢或者感觉有用的代码点赞,您的评价将有助于系统推荐出更棒的Python代码示例。
示例1: __init__
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import CreateFile [as 别名]
def __init__(self, filename):
self._hfile = win32file.CreateFile(filename,
win32con.GENERIC_READ | win32con.GENERIC_WRITE,
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
win32security.SECURITY_ATTRIBUTES(),
win32con.OPEN_EXISTING,
win32con.FILE_FLAG_OVERLAPPED,
0)
self._read_ovrlpd = pywintypes.OVERLAPPED()
self._read_ovrlpd.hEvent = win32event.CreateEvent(None, True,
False, None)
self._write_ovrlpd = pywintypes.OVERLAPPED()
self._write_ovrlpd.hEvent = win32event.CreateEvent(None, True,
False, None)
self._bufs = []
self._n = 0
示例2: _openHandle
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import CreateFile [as 别名]
def _openHandle(path, bWriteAccess, bWriteShare,
logfunc = lambda s: None):
TIMEOUT, NUM_RETRIES = 10, 20
for retry_count in range(6):
try:
access_flag = win32con.GENERIC_READ | \
(bWriteAccess and win32con.GENERIC_WRITE or 0)
share_flag = win32con.FILE_SHARE_READ | \
(bWriteShare and win32con.FILE_SHARE_WRITE or 0)
handle = win32file.CreateFile(
path, access_flag, share_flag, None,
win32con.OPEN_EXISTING, win32con.FILE_ATTRIBUTE_NORMAL, None)
nth = { 0: 'first', 1:'second', 2:'third'}
logfunc("Opening [%s]: success at the %s iteration" %
(path, nth.get(retry_count, '%sth' % (retry_count+1))))
return handle
except pywintypes.error as e:
logfunc('Exception=>'+str(e))
if NUM_RETRIES/3 < retry_count:
bWriteShare = True
time.sleep(TIMEOUT / float(NUM_RETRIES))
else:
raise RuntimeError("Couldn't open handle for %s." % path)
示例3: _OpenFileForRead
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import CreateFile [as 别名]
def _OpenFileForRead(self, path):
try:
fhandle = self.fhandle = win32file.CreateFile(
path,
win32file.GENERIC_READ,
win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE,
None,
win32file.OPEN_EXISTING,
win32file.FILE_ATTRIBUTE_NORMAL,
None)
self._closer = weakref.ref(
self, lambda x: win32file.CloseHandle(fhandle))
self.write_enabled = False
return fhandle
except pywintypes.error as e:
raise IOError("Unable to open %s: %s" % (path, e))
示例4: _OpenFileForWrite
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import CreateFile [as 别名]
def _OpenFileForWrite(self, path):
try:
fhandle = self.fhandle = win32file.CreateFile(
path,
win32file.GENERIC_READ | win32file.GENERIC_WRITE,
win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE,
None,
win32file.OPEN_EXISTING,
win32file.FILE_ATTRIBUTE_NORMAL,
None)
self.write_enabled = True
self._closer = weakref.ref(
self, lambda x: win32file.CloseHandle(fhandle))
return fhandle
except pywintypes.error as e:
raise IOError("Unable to open %s: %s" % (path, e))
示例5: SimpleFileDemo
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import CreateFile [as 别名]
def SimpleFileDemo():
testName = os.path.join( win32api.GetTempPath(), "win32file_demo_test_file")
if os.path.exists(testName): os.unlink(testName)
# Open the file for writing.
handle = win32file.CreateFile(testName,
win32file.GENERIC_WRITE,
0,
None,
win32con.CREATE_NEW,
0,
None)
test_data = "Hello\0there".encode("ascii")
win32file.WriteFile(handle, test_data)
handle.Close()
# Open it for reading.
handle = win32file.CreateFile(testName, win32file.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, 0, None)
rc, data = win32file.ReadFile(handle, 1024)
handle.Close()
if data == test_data:
print "Successfully wrote and read a file"
else:
raise Exception("Got different data back???")
os.unlink(testName)
示例6: testTransactNamedPipeBlocking
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import CreateFile [as 别名]
def testTransactNamedPipeBlocking(self):
event = threading.Event()
self.startPipeServer(event)
open_mode = win32con.GENERIC_READ | win32con.GENERIC_WRITE
hpipe = win32file.CreateFile(self.pipename,
open_mode,
0, # no sharing
None, # default security
win32con.OPEN_EXISTING,
0, # win32con.FILE_FLAG_OVERLAPPED,
None)
# set to message mode.
win32pipe.SetNamedPipeHandleState(
hpipe, win32pipe.PIPE_READMODE_MESSAGE, None, None)
hr, got = win32pipe.TransactNamedPipe(hpipe, str2bytes("foo\0bar"), 1024, None)
self.failUnlessEqual(got, str2bytes("bar\0foo"))
event.wait(5)
self.failUnless(event.isSet(), "Pipe server thread didn't terminate")
示例7: testTransactNamedPipeBlockingBuffer
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import CreateFile [as 别名]
def testTransactNamedPipeBlockingBuffer(self):
# Like testTransactNamedPipeBlocking, but a pre-allocated buffer is
# passed (not really that useful, but it exercises the code path)
event = threading.Event()
self.startPipeServer(event)
open_mode = win32con.GENERIC_READ | win32con.GENERIC_WRITE
hpipe = win32file.CreateFile(self.pipename,
open_mode,
0, # no sharing
None, # default security
win32con.OPEN_EXISTING,
0, # win32con.FILE_FLAG_OVERLAPPED,
None)
# set to message mode.
win32pipe.SetNamedPipeHandleState(
hpipe, win32pipe.PIPE_READMODE_MESSAGE, None, None)
buffer = win32file.AllocateReadBuffer(1024)
hr, got = win32pipe.TransactNamedPipe(hpipe, str2bytes("foo\0bar"), buffer, None)
self.failUnlessEqual(got, str2bytes("bar\0foo"))
event.wait(5)
self.failUnless(event.isSet(), "Pipe server thread didn't terminate")
示例8: testSimpleFiles
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import CreateFile [as 别名]
def testSimpleFiles(self):
try:
fd, filename = tempfile.mkstemp()
except AttributeError:
self.fail("This test requires Python 2.3 or later")
os.close(fd)
os.unlink(filename)
handle = win32file.CreateFile(filename, win32file.GENERIC_WRITE, 0, None, win32con.CREATE_NEW, 0, None)
test_data = str2bytes("Hello\0there")
try:
win32file.WriteFile(handle, test_data)
handle.Close()
# Try and open for read
handle = win32file.CreateFile(filename, win32file.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, 0, None)
rc, data = win32file.ReadFile(handle, 1024)
self.assertEquals(data, test_data)
finally:
handle.Close()
try:
os.unlink(filename)
except os.error:
pass
# A simple test using normal read/write operations.
示例9: setUp
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import CreateFile [as 别名]
def setUp(self):
self.watcher_threads = []
self.watcher_thread_changes = []
self.dir_names = []
self.dir_handles = []
for i in range(self.num_test_dirs):
td = tempfile.mktemp("-test-directory-changes-%d" % i)
os.mkdir(td)
self.dir_names.append(td)
hdir = win32file.CreateFile(td,
ntsecuritycon.FILE_LIST_DIRECTORY,
win32con.FILE_SHARE_READ,
None, # security desc
win32con.OPEN_EXISTING,
win32con.FILE_FLAG_BACKUP_SEMANTICS |
win32con.FILE_FLAG_OVERLAPPED,
None)
self.dir_handles.append(hdir)
changes = []
t = threading.Thread(target=self._watcherThreadOverlapped,
args=(td, hdir, changes))
t.start()
self.watcher_threads.append(t)
self.watcher_thread_changes.append(changes)
示例10: CopyFileToCe
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import CreateFile [as 别名]
def CopyFileToCe(src_name, dest_name, progress = None):
sh = win32file.CreateFile(src_name, win32con.GENERIC_READ, 0, None, win32con.OPEN_EXISTING, 0, None)
bytes=0
try:
dh = wincerapi.CeCreateFile(dest_name, win32con.GENERIC_WRITE, 0, None, win32con.OPEN_ALWAYS, 0, None)
try:
while 1:
hr, data = win32file.ReadFile(sh, 2048)
if not data:
break
wincerapi.CeWriteFile(dh, data)
bytes = bytes + len(data)
if progress is not None: progress(bytes)
finally:
pass
dh.Close()
finally:
sh.Close()
return bytes
示例11: open
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import CreateFile [as 别名]
def open(self, name):
"""
Direct open devices.
:param name: Port name.
:return: 0 on success
"""
path = self.ports[name]['path']
try:
self.files[path] = win32file.CreateFile(path,
win32file.GENERIC_WRITE |
win32file.GENERIC_READ,
0,
None,
win32file.OPEN_EXISTING,
win32file.FILE_ATTRIBUTE_NORMAL,
None)
except win32file.error as exc_detail:
print("%s\nFAIL: Failed open file %s" % (str(exc_detail), name))
return exc_detail
print("PASS: All files opened correctly.")
示例12: CreateFile
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import CreateFile [as 别名]
def CreateFile(self, fname, mode="r", bufsize=-1):
"Open a file the same way a File Directory migration engine would."
fname = cygwin2nt(fname)
UserLog.msg("CreateFile", fname)
if mode == "r":
wmode = win32file.GENERIC_READ
elif mode == "w":
wmode = win32file.GENERIC_WRITE
elif mode in ( 'r+', 'w+', 'a+'):
wmode = win32file.GENERIC_READ | win32file.GENERIC_WRITE
else:
raise ValueError, "invalid file mode"
h = win32file.CreateFile(
fname, # CTSTR lpFileName,
wmode, # DWORD dwDesiredAccess,
win32file.FILE_SHARE_DELETE | win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE, # DWORD dwShareMode,
None, # LPSECURITY_ATTRIBUTES lpSecurityAttributes,
win32file.OPEN_EXISTING, # DWORD dwCreationDisposition,
win32file.FILE_ATTRIBUTE_NORMAL, # DWORD dwFlagsAndAttributes,
0, # HANDLE hTemplateFile
)
self._files[int(h)] = h
return int(h)
示例13: __init__
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import CreateFile [as 别名]
def __init__(self, path):
if not isinstance(path, basestring):
raise TypeError("Path argument must be a basestring; instead"
" received %r" % (path,))
self.path = path
self.handle = win32file.CreateFile(
self.path,
0x0001, # FILE_LIST_DIRECTORY
win32con.FILE_SHARE_READ | win32con.FILE_SHARE_WRITE,
None,
win32con.OPEN_EXISTING,
win32con.FILE_FLAG_BACKUP_SEMANTICS | win32con.FILE_FLAG_OVERLAPPED,
None,
)
self.overlapped = win32file.OVERLAPPED()
self.overlapped.hEvent = win32event.CreateEvent(None, True, 0, None)
示例14: connect
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import CreateFile [as 别名]
def connect(self):
"""Attempts to connect to the server, but does not block.
@return: True if the channel is now connected.
@rtype: bool
"""
try:
if win32pipe.WaitNamedPipe(self.__full_pipe_name, 10) != 0:
self.__pipe_handle = win32file.CreateFile(
self.__full_pipe_name,
win32file.GENERIC_READ,
0,
None,
win32file.OPEN_EXISTING,
0,
None,
)
return True
else:
return False
except pywintypes.error as e:
if e[0] == winerror.ERROR_FILE_NOT_FOUND:
return False
else:
raise e
示例15: csv_export_ram
# 需要导入模块: import win32file [as 别名]
# 或者: from win32file import CreateFile [as 别名]
def csv_export_ram(self):
"""Dump ram using winpmem"""
hSvc = create_driver_service(self.logger)
start_service(hSvc, self.logger)
try:
fd = win32file.CreateFile(
"\\\\.\\pmem",
win32file.GENERIC_READ | win32file.GENERIC_WRITE,
win32file.FILE_SHARE_READ | win32file.FILE_SHARE_WRITE,
None,
win32file.OPEN_EXISTING,
win32file.FILE_ATTRIBUTE_NORMAL,
None)
try:
t = time.time()
image = _Image(fd)
self.logger.info("Imaging to " + self.output_dir + '\\' + self.computer_name + '_memdump.raw')
image.DumpWithRead(self.output_dir + '\\' + self.computer_name + '_memdump.raw')
self.logger.info("Completed in %s seconds" % (time.time() - t))
finally:
win32file.CloseHandle(fd)
finally:
stop_and_delete_driver_service(hSvc)