Python語言個人學習記錄
第一種python有一系列API預設直接可以引用的函式,這裡檔案讀取函式open在下列表
The Python interpreter has a number of functions and types built into it that are always available.
They are listed here in alphabetical order.
第一種python有一系列API預設直接可以引用的函式,這裡檔案讀取函式open在下列表
The Python interpreter has a number of functions and types built into it that are always available.
They are listed here in alphabetical order.
使用示例,這裡開啟demo.txt檔案,位於當前目錄下。自己也可以定義。
程式碼裡面有兩種讀取方式第一種使用readline第二種使用readlines特別注意
# version v3.4 # date 2014-09-25 import builtins # api # demo 0 DataArray = [] DataSize = 0 if __name__ == '__main__': print('main :') print('-----------read---line-----------') File_Read = builtins.open('demo
執行結果如下:
main : -----------read---line----------- data size in bytes : 558 aricwise9898eman0990 hopelessfo5665ol7676 diegodavty6565i8223 phamvi2328989etcu2332 ale.bosrerereco21211223 shadae.freeman3232232sdsa alexperry82fdfdq32 emhtyail9fdf263fd2ade devianai232332dzdz aricwise9898eman8787 hopelessfo5665ol5445 diegodavty6565i82323 phamvi2328989etcuwqwq ale.bosrerereco21q shadae.freeman3232weew alexperry82fdfdkllk emhtyail9fdf263fd2rttr devianai232332gfgf aricwise9898emang hopelessfo5665ol609 diegodavty6565i8278 phamvi2328989etcu0- ale.bosrerereco2190 shadae.freeman323982 alexperry82fdfd45 emhtyail9fdf263fd452 devianai232332334 -----------read---lines----------- aricwise9898eman0990 hopelessfo5665ol7676 diegodavty6565i8223 phamvi2328989etcu2332 ale.bosrerereco21211223 shadae.freeman3232232sdsa alexperry82fdfdq32 emhtyail9fdf263fd2ade devianai232332dzdz aricwise9898eman8787 hopelessfo5665ol5445 diegodavty6565i82323 phamvi2328989etcuwqwq ale.bosrerereco21q shadae.freeman3232weew alexperry82fdfdkllk emhtyail9fdf263fd2rttr devianai232332gfgf aricwise9898emang hopelessfo5665ol609 diegodavty6565i8278 phamvi2328989etcu0- ale.bosrerereco2190 shadae.freeman323982 alexperry82fdfd45 emhtyail9fdf263fd452 devianai232332334 exit
二
第二種這種檔案讀取採用linecache操作模組。該模組總共有以下五個函式
linechche是一種高校區文字檔案模組。從檔案獲取文字行,維護一個結果快取,
從而實現高效的讀取多行文字。其中可以快速的讀取指定的某一行。
示例程式碼:
def getline(filename, lineno, module_globals=None): def clearcache(): def getlines(filename, module_globals=None): def checkcache(filename=None): def updatecache(filename, module_globals=None):
示例程式碼如下,這裡既使用getline函式也使用了getlines函式。
其中區別讀者自己除錯觀察。
示例程式碼:
import linecache # demo 1 if __name__ == '__main__': print('main :') line = linecache.getline('demo.txt', 13) if line == '': print('index out of range') else: print(line) lines = linecache.getlines('demo.txt') for lineIndex in lines: print(lineIndex) linecache.clearcache() print('exit')
執行結果如下:
main : phamvi2328989etcuwqwq aricwise9898eman0990 hopelessfo5665ol7676 diegodavthttp://tooschip.com6565i8223 phamvi2328989etcu2332 ale.bosrerereco21211223 shadae.freeman3232232sdsa alexperry82fdfdq32 emhtyail9fdf263fd2ade devianai232332dzdz aricwise9898eman8787 hopelessfo5665ol5445 diegodavty6565i82323 phamvi2328989etcuwqwq ale.bosrerereco21q shadae.freeman3232weew alexperry82fdfdkllk emhtyail9fdf263fd2rttr devianai232332gfgf aricwise9898emang hopelessfo5665ol609 diegodavty6565i8278 phamvi2328989etcu0- ale.bosrerereco2190 shadae.freeman323982 alexperry82fdfd45 emhtyail9fdf263fd452 devianai232332334 exit
三
第三種方式採用tempfile模組實現。
tempfile臨時檔案系統物件。python使用tempfile建立
一系列安全的臨時檔案系統資源
主要函式如下。這裡我們測試一下它的臨時匿名檔案和命名檔案函式
TemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix='', prefix='tmp', dir=None) TemporaryDirecthttp://caferacerforum.comry(suffix='', prefix='tmp', dir=None) NamedTemporaryFile(mode='w+b', buffering=None, encoding=None, newline=None, suffix='', prefix='tmp', dir=None, delete=True) SpooledTemporaryFile(max_size=0, mode='w+b', buffering=None, encoding=None, newline=None, suffix='', prefix='tmp', dir=None) mktemp(suffix='', prefix='tmp', dir=None)
示例程式碼:
import tempfile import os if __name__ == '__main__': print('main :') with tempfile.TemporaryFile('w+t') as tmpNoNameFile: tmpNoNameFile.write('a11111111111111') tmpNoNameFile.write('a22222222222222') tmpNoNameFile.write('a33333333333333') tmpNoNameFile.write('a44444444444444') tmpNoNameFile.seek(0) print('handle----', tmpNoNameFile) for tmp in tmpNoNameFile: print(tmp) tmpNoNameFile.close() with tempfile.NamedTemporaryFile('w+t') as tmpNameFile: tmpNameFile.write('x11111111111111') tmpNameFile.write('x22222222222222') tmpNameFile.write('x33333333333333') tmpNameFile.write('x44444444444444') tmpNameFile.seek(0) print('handle----', tmpNameFile) for tmp in tmpNameFile: print(tmp) tmpNameFile.close() tmpDir = tempfile.mkdtemp() print(tmpDir) os.removedirs(tmpDir) print(tmpDir) print('exit')
執行結果如下:
main : handle---- <tempfile._TemporaryFileWrapper object at 0x0000000002CCDF98> a11111111111111a22222222222222a33333333333333a44444444444444 handle---- <tempfile._TemporaryFileWrapper object at 0x0000000002FF3748> x11111111111111x22222222222222x33333333333333x44444444444444 C:UsersROOTAppDataLocalTemp mpkgijyiqb C:UsersROOTAppDataLocalTemp mpkgijyiqb exit
這裡說明一下,臨時檔案系統資源,會在檔案關閉時候自動刪除。
但是目錄不行,必須自己手動刪除,注意這行程式碼
<span style="font-size:12px;">os.removedirs(tmpDir)</span>四
第四種是檔案操作是檔案的拷貝直接利用shutil模組提供的相關函式即可。
這個模組可以做一些高階操作,比如設定檔案的一些屬性和許可權。
示例程式碼:
import shutil import os if __name__ == '__main__': print('main :') try: shutil.copy('demo.txt', 'des.txt') except IOError: print(IOError.errno) os.chmod('demo.txt', 0x444) print('exit')
五
第五種使用的記憶體對映檔案,類似於windows的對映。
記憶體對映通常可以提供I/O效能,因為使用記憶體對映時,不會對每個訪問都有一個
單獨的系統呼叫,而且不需要緩衝區進行復制資料,可以再核心和使用者之間方便的共享資料
和記憶體。
示例程式碼:
import builtins import mmap import contextlib if __name__ == '__main__': print('main :') file = builtins.open('demo.txt', 'r') with contextlib.closing(mmap.mmap(file.fileno(), 0, access=mmap.ACCESS_READ)) as mm: print(mm.read(10)) print(mm[:100]) print('exit')
執行結果如下:
main : b'aricwise98' b'aricwise9898eman0990 hopelessfo5665ol7676 diegodavty6565i8223 phamvi2328989etcu2332 ale.bosrerer' exit
各類量化機器人系統開發:17166570329