Python 小社-6

File I/O

不出意外馬上就出意外了

所以我們這周上檔案讀寫

怎麼打開檔案

#var = open('file_name', 'open_mode')
file = open('data.txt', 'r')

打開檔案以後 我們就可以對這個檔案操做了

檔案路徑

檔案路徑分為相對路徑和絕對路徑

這裡的相對是相對你正在操作的python檔案

絕對路徑則是指這個檔案在你電腦的位置

在操作檔案的時候記得要注意她的位置

檔案路徑

open('file1.txt')
open('/folder/file2.txt')
open('./folder/file3.txt')
open('../folder/file4.txt')
open('C:\\user\\file5.txt')

#用上面五行開啟的檔案位置大概像這樣

C:\user\file\file1.txt
C:\folder\file2.txt
C:\user\file\folder\file3.txt
C:\user\folder\file4.txt
C:\user\file5.txt

開啟檔案的模式

mode 功能
r 讀取(沒有檔案時錯誤)
w 寫入(清空原內容)
x 以寫入模式開新檔案
a 從檔案EOF開始寫入

額外模式

mode 功能
+ 讀寫模式
b 二進位模式

二進位模式

有沒有想過

為什麼開啟doc檔的時候需要用word開

但是開啟txt檔只要用任意的文字編輯器呢

那是因為不是所有檔案都是純文字檔

像doc, excel, jpg等都是二進制檔案

with open('xxx.bin', 'rb') as f:
    data = f.read()

for i in range(len(data)):
    print(hex(data[i]) + ' ', end='') #0x68 0x65 0x6c 0x6c 0x6f

encoding

電腦在處理文字的時候有不同的編碼方式

使用到錯誤的編碼會產生亂碼

目前國際通用的編碼方式是UTF-8

file = open('data.txt', 'r', encoding='utf-8')

write

就是寫入

把參數的字串寫入檔案

f = open('data2.txt', 'r')
f.write('hello ')
f.write('world!')
#data2.txt
hello world

read

括號裡的參數代表要讀的字元數

如果沒放的話就是讀到EOF為止

終於有一頁簡報字數是整齊的了

#python
f = open('data2.txt', 'r')
print(f.read(6)) #hello
print(f.read(5)) #world
print(f.read( ))
#data2.txt
hello world

close

用完檔案之後要把檔案關掉

不然會一直占用記憶體

f = open('data2.txt', 'r')
print(f.read(6)) #hello
print(f.read(5)) #world
print(f.read( ))
f.close()

with open as

誒 可是我都會忘記要關檔案誒

只要用with open as

檔案在離開這個區塊就會自動關掉了

with open('data2.txt', 'r+') as f:
  print(f.read())
  
#關掉了

游標

剛剛在講read的時候有沒有發現一件事

#python
f = open('data2.txt', 'r')
print(f.read(6)) #hello
print(f.read(5)) #world
print(f.read( ))

為什麼第四行不是hello

第五行不是hello world

游標

hello world I am python

游標

hello world I am python

read(6)

游標

hello world I am python

read(5)

游標

hello world I am python

read( )

游標

hello world I am python

tell()

23

游標

hello world I am python

seek(6)

游標

hello world I am python

seek(17)

游標

hello world I am FileIO

write(FileIO)

readline

讀一整行

#data2.txt
hello
world
#python
with open('data2.txt', 'r+') as f:
  print(f.readline()) #hello
  print(f.readline()) #world

readlines

全部讀完 存到list裡面

#data2.txt
hello
world
#python
with open('data2.txt', 'r+') as f:
  a = f.readlines()
  print(a) #['hello', 'world']

學完這些程式

以後老師叫你們寫一個從1數到100的程式

就能利用這段程式碼 把答案給他了

#python
f = open('count.py', 'w+')
for i in range(1, 101):
    f.write(f'print({i})\n')
f.close()
#count.py
print(1)
print(2)
print(3)
print(4)
print(5)
print(6)
print(7)
print(8)
print(9)
print(10)
print(11)
print(12)
print(13)
print(14)
print(15)
print(16)
print(17)
print(18)
print(19)
print(20)
print(21)
print(22)
print(23)
print(24)
print(25)
print(26)
print(27)
print(28)
print(29)
print(30)
print(31)
print(32)
print(33)
print(34)
print(35)
print(36)
print(37)
print(38)
print(39)
print(40)
print(41)
print(42)
print(43)
print(44)
print(45)
print(46)
print(47)
print(48)
print(49)
print(50)
print(51)
print(52)
print(53)
print(54)
print(55)
print(56)
print(57)
print(58)
print(59)
print(60)
print(61)
print(62)
print(63)
print(64)
print(65)
print(66)
print(67)
print(68)
print(69)
print(70)
print(71)
print(72)
print(73)
print(74)
print(75)
print(76)
print(77)
print(78)
print(79)
print(80)
print(81)
print(82)
print(83)
print(84)
print(85)
print(86)
print(87)
print(88)
print(89)
print(90)
print(91)
print(92)
print(93)
print(94)
print(95)
print(96)
print(97)
print(98)
print(99)
print(100)

python-6 file IO

By ck1110793

python-6 file IO

  • 169