Win32file Module概述
Native File Manipulation定血。
There are times when the standard Python file objects can't meet your requirements, and you need to use the Windows API to manipulate files.
當(dāng)通用的文件操作方法(就是python里面的open毅弧、read睦番、write)無法滿足特殊需求(安全性啊啥的~)時(shí)欢际,這個(gè)時(shí)候windows操作文件的API就是時(shí)候發(fā)揮效果了母市!
This can happen in a number of situations, such as:
? You need to read or write data to or from a Windows pipe.
你需要從一個(gè)windows管道讀出或者寫入數(shù)據(jù)時(shí)。
? You need to set custom Windows security on a file you are creating.
需要設(shè)定特殊文件的安全性時(shí)损趋。
? You need to perform advanced techniques for performance reasons, such as "Overlapped" operations or using completion ports.
Completion ports利用一些線程患久,幫助平衡由I/O請(qǐng)求所引起的負(fù)載
OVERLAPPED I/O(異步I/O)是一個(gè)包含了用于異步輸入輸出的信息的結(jié)構(gòu)體
總之:就是你要提高文件操作性能的時(shí)候用這些。
win32file.WriteFile()
returns the error code from the operation.
This is either zero or win32error.ERROR_IO_PENDING if overlapped I/O is used.
All other error codes are converted to a Python exception.
The win32file.CreateFile() function opens or creates standard files,
returning a handle to the file.
Standard files come in many flavors, including synchronous files
(where read or write operations don't return until the
operation has completed); asynchronous (or overlapped I/O) files,
where read and write operations return immediately;
and temporary files that are automatically deleted when the handle is closed.
Files may also be opened requesting that
Windows not cache any file operations, that no buffering is performed, etc.
This function returns a PyHANDLE object.
PyHANDLEs are simply objects that wrap standard Win32 HANDLEs.
When a PyHANDLE object goes out of scope, it's automatically closed;
thus, it's generally not necessary to close
these HANDLEs as it is necessary when using these from C or C++.
Overlapped I/O
Windows provides a number of techniques for high-performance file I/O.
The most common is overlapped I/O. Using overlapped I/O,
the win32file.ReadFile() and win32file.WriteFile() operations are asynchronous
and return before the actual I/O operation has completed.
When the I/O operation finally completes, a Windows event is signaled.
Overlapped I/O does have some requirements normal I/O operations don't:
? The operating system doesn't automatically advance the file pointer.
When not using overlapped I/O, a ReadFile or WriteFile operation automatically
advances the file pointer, so the next operation automatically reads the subsequent
data in the file. When usingoverlapped I/O, you must manage the location
in the file manually.
? The standard technique of returning a Python string object from
win32file. ReadFile() doesn't work. Because the I/O operation has not
completed when the call returns, a Python string can't be used.
As you can imagine, the code for performing overlapped I/O is more complex than
when performing synchronous I/O. Chapter 18, WindowsNT Services,
contains some sample code that uses basic
overlapped I/O on a Windows-named pipe.
Win32file Module解析
The win32file module contains functions that interface to the File and other I/O-related Win32 API functions.
CreateFile(FileName,········)
Opens or creates a file or a number of other objects and returns a handle that can access the object.
FileName
The name of the file, pipe, or other resource to open.
Result
A PyHANDLE object to the file.
ReadFile()
Reads data from an open file.
errCode, data = ReadFile(FileHandle, Size, Overlapped)
FileHandle
The file handle identifying the file to read from. This handle typically is obtained from win32file.CreateFile().
Size or ReadBuffer
If Size is specified as a Python integer, it's the number of bytes to read from the file. When using overlapped I/O, a ReadBuffer shouldbe used, indicating where the ReadFile operation should place the data.
這句話的意思是:將如你放的是一個(gè)整型數(shù)據(jù),那么readfile()啊蒋失,你得從這個(gè)文件里讀這么多的數(shù)據(jù)回來返帕,但是一旦是異步文件讀取的話,你必須指定一個(gè)buffer高镐,你想案刃:
- 正常的同步文件讀取流程是這樣的畸冲,你給我把數(shù)據(jù)挖回來嫉髓,我等著,然后底層I/O操作就吭哧吭哧把數(shù)據(jù)給挖回來了(同時(shí)你一直在礦井口即
FileHandle
這等著)邑闲,然后把數(shù)據(jù)放到一個(gè)叫做data的“籮筐”內(nèi)~交接完畢算行! - 但是異步讀取是這么一個(gè)流程:一天一個(gè)商人找到一個(gè)包工頭(應(yīng)用程序),說:啊苫耸,小劉爸菪稀(readfile())啊,你給我去
FileHandle
這個(gè)礦井挖一籮筐礦回來褪子!接收到指令后量淌,小劉(readfile())就去找到挖礦工鐵柱(系統(tǒng)底層驅(qū)動(dòng)),說:鐵柱啊嫌褪,你給我去FileHandle
這個(gè)礦井挖一筐礦上來呀枢,然后就去干其他事了(走的時(shí)候給了鐵柱一部手機(jī)(overlapped),說“整完了給我電話笼痛,我來收”)裙秋,但是這其中有個(gè)問題啊,鐵柱整好的礦往哪放坝б痢摘刑?同步模式下,鐵柱冒出礦井就可以看到劉工頭在井口等著刻坊,直接往框內(nèi)一放枷恕,就交接完事了,但是這個(gè)不一樣啊谭胚,鐵柱只有在出礦井后才有信號(hào)活尊,這個(gè)時(shí)候打電話給劉工頭,然后就是干等著漏益,指不定工頭啥時(shí)候來呢蛹锰!說不定等的這段時(shí)間都可以再挖一卡車礦了呢!绰疤!說不定等著等著鐵柱都快餓掛了铜犬!而且可以肯定的是這個(gè)時(shí)間內(nèi)其他包工頭不能給鐵柱派活了!!總之就是浪費(fèi)了大量的資源癣猾;因此我們就想啊敛劝,要是我們?cè)诮需F柱下去挖礦的時(shí)候順便告訴它你挖完后直接放到我們指定的地點(diǎn),然后通知我們纷宇,我們就會(huì)來取的(取到data這個(gè)“籮筐”內(nèi))夸盟,放到指定地點(diǎn)后你想干嘛干嘛去,大大提高你的效率像捶!畢竟整個(gè)礦場就靠著(基于)你們這群底層的干活人員來運(yùn)轉(zhuǎn)呢上陕! 科科~
感悟:這其實(shí)就是一個(gè)現(xiàn)代商業(yè)社會(huì)微劇場的縮影,在實(shí)際場景中人們總是很容易的想到優(yōu)化的方式拓春,并且留心生活的極客們?cè)缇桶焉顚懭肓舜a释簿,優(yōu)化了代碼!因此我們平時(shí)留心注意生活硼莽,學(xué)習(xí)生活中的智慧庶溶、優(yōu)化思維然后再拓展發(fā)散到各行各業(yè)各方個(gè)面,余以為這是一種可行的策略懂鸵。
Overlapped
An OVERLAPPED object or None if overlapped I/O isn't being performed. This parameter can be omitted, in which case None is used.
Result
errCode:Either 0, or ERROR_IO_PENDING.
Data:The data read from the file.
這里的data就是上面描述的“籮筐”偏螺。異常代碼這里是兩種要么0要么就是異步I/O掛起提示,0表示數(shù)據(jù)獲取成功匆光,I/O掛起是異步文件讀取的時(shí)候套像,提前返回時(shí)的返回碼。
這里的中層哲學(xué)是:不要想管太多(比如殴穴,我會(huì)很“貪心”地想返回值可以返回異常狀態(tài)凉夯,也許是有的,因?yàn)楫吘怪幻枋隽?是成功采幌,其他字段并沒有描述劲够,這部分得看源碼),這只care成功休傍,其他的除了異步態(tài)下的 ERROR_IO_PENDING就都是異常了征绎,你開發(fā)商只要知道0代表成功了,其他的(除去ERROR_IO_PENDING)都是異常就好了磨取,具體的人柿,你精力旺盛想知道是什么異常也可以去查的,反正我作為一個(gè)包工頭對(duì)上(開發(fā)商)是提供高效率的接口 和 高精度的搜查(可選)忙厌,這想必就是嚴(yán)謹(jǐn)?shù)?strong>做事態(tài)度吧Y灬!逢净!對(duì)下(鐵柱)哥放,合理安排策略使得底層利益最大化歼指,實(shí)現(xiàn)共贏。
WriteFile()
Writes data to an open file.
errCode = WriteFile(FileHandle, Data, Overlapped)
FileHandle
The file handle identifying the file to read from. This handle typically is obtained from win32file.CreateFile().
Data
The data to write to the file, as a Python string.
Overlapped
An OVERLAPPED object or None if overlapped I/O isn't being performed. This parameter can be omitted, in which case None is used.
Result
errCode: Either 0 or ERROR_IO_PENDING.