1. 程式人生 > >python-檔案操作(1)

python-檔案操作(1)

本文內容涉及python開啟/建立檔案物件,檔案的讀寫、檔案指標位置的移動、獲取命令列引數。

1. open()
open函式以指定模式返回一個file物件,如: file_object = open(filename,access_mode=’r',buffering=-1),預設是以r模式開啟檔案。

filename:表示要開啟檔名(字串),可以是絕對路徑或相對路徑
access_mode:檔案開啟的模式(字串), 常用的模式有’r',’w',’a',不是很常用的還有’u'和’b’
‘r’模式:以讀方式開啟,不能進行寫操作,檔案必須是已經存在的
‘r+’模式:以讀寫方式開啟,檔案必須是已經存在的
‘w’模式:以寫方式開啟,不能進行讀操作,若檔案存在,則先清空,然後重新建立;若不存在,則建立檔案
‘w+’模式:以讀寫方式開啟,若檔案存在,則先清空,然後重新建立;若不存在,則建立檔案
‘a’模式:以追加方式開啟,不能進行讀操作,把資料追加到檔案的末尾;若不存在,則建立檔案
‘a+’模式:以讀寫方式開啟,把資料追加到檔案的末尾;若不存在,則建立檔案
‘b’模式:以二進位制模式開啟,不能作為第一個字元出現,需跟以上模式組合使用,如’rb’,'rb+’等,
‘u’模式:表示通用換行符支援,檔案必須是已經存在的

buffering:表示訪問檔案採用的緩衝方式,0表示不緩衝,1表示緩衝一行資料,其他大於1的值表示使用給定值作為緩衝區大小,負數表示使用系統預設緩衝機制,預設是-1,一般使用系統預設方式。

2. file()
file是一個類,file()以指定模式建立一個file物件,跟open()具有相同的功能,可以任意替換。一般使用open來建立一個file物件,使用isinstance(obj,file)來判斷obj是否是一個檔案物件。

3.read()、readline()、readlines()
read():讀取指定數目個位元組到字串中,負數將讀取至檔案末尾,預設是-1

>>> file_obj = open('test.txt','r')
>>> file_obj.read()
'dfdff\n'
>>> file_obj.seek(0)
>>> file_obj.read(0)
''
>>> file_obj.read(1)
'd'
>>> file_obj.read(2)
'fd'
>>> file_obj.read(-1)
'ff\n'
>>> file_obj.seek(0)
>>> file_obj.read(1000)
'dfdff\n'
>>> file_obj.seek(0)
>>> file_obj.read(-5)
'dfdff\n'
readline():讀取檔案的一行,包括行結束符,可以制定size引數的值,預設是-1
>>> file_obj = open('test.txt','r')
>>> file_obj.readline()
'dfdff\n'
>>> file_obj.readline(2)
'al'
>>> file_obj.readline(-1)
'exzhou\n'

readlines():讀取所有剩餘的行,然後作為一個字串列表返回
>>> file_obj.seek(0)
>>> file_obj.readlines()
['dfdff\n', 'alexzhou\n', 'zhoujianghai\n']

4. write()、writelines()
ps:這兩個方法都不會自動加上行結束符,需在寫入資料前自己加上
>>> file_obj = open('test.txt','w+')
>>> file_obj.write('alexzhou')
>>> file_obj.write(' python')
>>> file_obj.seek(0)
>>> file_obj.readline()
'alexzhou python'
 
>>> l = ['my','name','is','zhoujianghai']
>>> l = ' '.join(l)
>>> file_obj.writelines(l)
>>> file_obj.seek(0)
>>> file_obj.readline()
'alexzhou pythonmy name is zhoujianghai'
 
>>> file_obj.write('hello \n')
>>> file_obj.write('world \n')
>>> file_obj.seek(0)
>>> file_obj.readline()
'alexzhou pythonmy name is zhoujianghaihello \n'
>>> file_obj.readline()
'world \n'

5. seek()、tell()
seek():移動檔案指標到不同的位置,可以指定偏移量和起始位置。起始位置0表示從檔案頭開始,1表示從當前位置開始,2表示從檔案尾開始,預設是0.
tell():表示當前檔案指標在檔案中的位置,從檔案頭算起。
>>> file_obj.seek(0)
>>> file_obj.tell()
0
>>> file_obj.seek(5)
>>> file_obj.tell()
5
 
>>> file_obj.seek(5,1)
>>> file_obj.tell()
10
>>> file_obj.seek(5,2)
>>> file_obj.tell()
57
>>> file_obj.seek(5,0)
>>> file_obj.tell()
5

6. 檔案迭代和關閉檔案
可以使用for迴圈一行一行讀取檔案
>>> file_obj = open('test.txt','w+')
>>> file_obj.write('hello \n')
>>> file_obj.write('world \n')
>>> file_obj.seek(0)
>>> for eachline in file_obj:
...     print eachline,
... 
hello 
world
>>> file_obj.close()

ps:print後面加一個分號的作用:避免print語句預設在列印的內容後面加一個換行符號。

7. os模組常用屬性
由於各作業系統的行分隔符和檔案分隔符不一樣,所以可以使用os模組的以下屬性避免程式碼移植時碰到這些問題。

os.linesep 行分隔符字串
os.sep 檔案分隔符字串
os.pathsep 路徑分隔符字串
os.curdir 當前目錄字串
os.pardir 父目錄字串

看下面的列印結果

>>> import os
>>> os.sep
'/'
>>> os.linesep
'\n'
>>> os.pathsep
':'
>>> os.curdir
'.'
>>> os.pardir
'..'

8. 獲取命令列引數
建立argv.py檔案,輸入下面程式碼
import sys
 
commands = sys.argv
print commands

執行:pyton argv.py 123,列印結果:

['argv.py', '123']