八 讀寫檔案

2021-10-06 04:03:57 字數 3300 閱讀 3507

關於《python程式設計快速上手-讓繁瑣工作自動化》的學習筆記

檔案有兩個關鍵屬性:「檔名」和「路徑」。路徑指明了檔案在計算機上的位置,路徑中的c:\部分是「根資料夾」,它包含了所有其它檔案(windows中根資料夾名為c:\,也稱c:盤,在osx和linux中根資料夾是/);檔名中最後乙個句點之後的部分稱為檔案的「副檔名」。(資料夾名稱和檔名在windows和osx上不區分大小寫,但在linux上區分大小寫)

>>> import os

>>> os.path.join('user','bin','spam')

'user\\bin\\spam' #每個倒斜槓需要倒斜槓來轉義

>>> myfiles=['accounts.txt','details.csv','invite.docx']

>>> for filename in myfiles:

print(os.path.join('c:\\users\\asweigart',filename))

c:\users\asweigart\accounts.txt

c:\users\asweigart\details.csv

c:\users\asweigart\invite.docx

>>> os.getcwd()

'c:\\users\\g3'

>>> os.chdir('c:\\windows')

>>> os.getcwd()

'c:\\windows'

使用os.makedirs()建立新資料夾

為確保完整路徑名存在,os.makedirs()將建立所有必要的中間資料夾

import os

>>> os.makedirs('c:\\users\\g3\\test')

>>> os.path.basename('c:\\users\\g3\\aapt.exe')

'aapt.exe'

>>> os.path.dirname('c:\\users\\g3\\aapt.exe')

'c:\\users\\g3'

>>> os.path.split('c:\\users\\g3\\aapt.exe')

('c:\\users\\g3', 'aapt.exe')

>>> 'c:\\users\\g3\\aapt.exe'.split(os.path.sep)

['c:', 'users', 'g3', 'aapt.exe']

os.path模組提供一些函式返回乙個相對路徑的絕對路徑以及檢查給定的路徑是否為絕對路徑

檢查路徑有效性

>>> os.path.isdir('c:\test')

true

>>> os.path.isdir('c:\test\test1')

true

>>> os.path.isdir('c:\test\test1\test2.txt')

false

>>> os.path.exists('g:\\')

false

>>> os.path.exists('g:\\')

true

python讀寫檔案有三個步驟:

1.呼叫open()函式,返回乙個file物件

2.呼叫file物件的read()或write()方法

3.呼叫file物件的close()方法關閉該檔案

>>> a=open(r'c:\test\test1.txt')

>>> a.read()

'1224325'

>>> b=open(r'c:\test\test2.txt')

>>> b.readlines()

['1224325\n', 'hello\n', 'world\n', 'nice\n', '!!!']

>>> file=open('c:\\test\\test3.txt','a')

>>> file.write('abcdefghijklmn\n')

15>>> file.close()

>>> file=open('c:\\test\\test3.txt','a')

>>> file.write('123456789\nhello world!\nthanks\n')

30>>> file.close()

>>> file=open('c:\\test\\test3.txt','r')

>>> content=file.read()

>>> print(content)

abcdefghijklmn

123456789

hello world!

thanks

>>>

利用shelve模組,可以將變數儲存到二進位制的shelf檔案中。呼叫shelve.open()函式並傳入乙個檔名,然後返回的值儲存在乙個變數中,完成相應的修改後呼叫close()。對於二進位制檔案不必使用讀或寫模式開啟,因為它們在開啟後既能寫又能讀(shelf值有keys()和values()方法,返回shelf中鍵和值的類似列表但不是真正的列表)

>>> import shelve

>>> shelffile=shelve.open('mydata')

>>> cats=['zophine','pooka','simon']

>>> shelffile['cats']=cats

>>> shelffile.close()

>>> shelffile=shelve.open('mydata')

>>> type(shelffile)

>>> shelffile['cats']

['zophine', 'pooka', 'simon']

>>> shelffile.close()

>>> shelffile=shelve.open('mydata')

>>> list(shelffile.keys())

['cats']

>>> list(shelffile.values())

[['zophine', 'pooka', 'simon']]

>>> shelffile.close()

pprint.pformat()函式返回乙個易於閱讀且語法正確的python字串

Python學習(八) 檔案操作 讀 寫

1 f open 歌詞 encoding utf 8 2 data f.read 3print data 4f.close 5 1126 234457 1233558 153454451515 另一種方式,不需自己close 1 with open a.txt w as f 2 f.write jg...

c 檔案讀寫 文字讀寫

include int main else return 0 格式 intfscanf file stream,constchar format,返回值 如果成功,該函式返回成功匹配和賦值的個數。如果到達檔案末尾或發生讀錯誤,則返回 eof 引數1 file stream 檔案指標 引數2 cons...

CSharp學習筆記之八 檔案的讀寫

在我們的程式的編寫過程中,總避免不了對檔案的讀寫,比如說要讀取乙個軟體的配置,這時候不就需要我麼來設計的檔案的讀寫麼,但是對於c 來說,是如何對檔案進行讀寫的呢。下面的我們來看乙個列子 class test catch system.exception ex trycatch system.exce...