Python學習part4 Python的檔案操作

2021-09-29 11:54:35 字數 4237 閱讀 9718

首先我們來看一道題

1.統計this.txt檔案中字母(『a』-『z』)、大寫字母( 『a』-『z』)、數字(0-9)、其他字元的個數。

我們可以用兩種方法來完成

1.普通方法

l =

0a =

0b =

0d =

0with

open

('c:/users/admin/desktop/text/this.txt'

,'r'

)as fr:

txt = fr.read(

)for c in txt:

if'a'

<=c<=

'z':

l +=

1elif

'a'<=c<=

'z':

a +=

1elif

'0'<=c<=

'9':

b +=

1else

: d +=

1print

('小寫字母:'

,l)print

('大寫字母:'

,a)print

('數字字元:'

,b)print

('其他:'

,d)

with 語句(上下文管理器)with 關鍵字(上下文管理器)可以自動管理上下文資源,不論什麼原因跳出 with 塊,都能

確保檔案正確的關閉,並且可以在**塊執行完畢後自動還原進入該**塊時的現場。

with

open

("檔案路徑","開啟方式"

)as f:

txt=f.read(

)

記得加分號!

附上開啟方式

2.運用isdigit(),isalpha()函式

intcount =

0#用來記錄列表中的int元素個數

strcount =

0#記錄str元素個數

othercount =

0with

open

('c:/users/admin/desktop/text/this.txt'

)as f:

#open用於開啟檔案,read用於讀取檔案

a=f.read(

)# 使用for迴圈遍歷字串,每次迴圈判斷當前獲取的元素的型別,並給對應計數器計數

for i in a:

if i.isdigit():

#判斷i是不是int

intcount +=

1elif i.isalpha():

#判斷i是不是str

strcount +=

1else

: othercount +=

1print

("字母:"

,strcount)

print

("數字:"

,intcount)

print

("其他:"

,othercount)

isdigit() 方法檢測字串是否只由數字組成。

isalpha() 方法檢測字串是否只由字母組成。

3.可以輸出字母和數字的re函式

import re

with

open

('c:/users/admin/desktop/text/this.txt'

)as f:

img=f.read(

)num=re.

compile

('[0-9]'

)#compile指編譯

alpha=re.

compile

('[a-z a-z]'

)numlist=num.findall(img)

alphalist=alpha.findall(img)

print

('包含數字:',numlist)

print

('包含字母:'

,alphalist)

下面介紹sys.path
import sys

'd:/hzh'

)#若要匯入自建檔案,必須加這個

import jmu

print

(jmu)

os模組常用方法

os.getcwd() 方法用於返回當前工作目錄。

os.chdir() 方法用於改變當前工作目錄到指定的路徑。

os.rename() 方法用於命名檔案或目錄,從 src 到 dst,如果dst是乙個存在的目錄, 將丟擲oserror。(需要在當前目錄開啟終端)

os.listdir() 方法用於返回指定的資料夾包含的檔案或資料夾的名字的列表。這個列表以字母順序。

os.path.abspath(path) 返回絕對路徑

os.path.basename(path) 返回檔名

write()/writelines()寫入資料

write指輸入字串;

writeline指將字串列表輸入

a=

open

('c:/users/admin/desktop/text/hhh(2).txt'

,'w'

)a.write(

'good\nhhh\njjj\n'

)a.writelines(

['abc\n'

,'def\n'

,'ghj\n'

])

文字檔案的讀取

with

open

('c:/users/admin/desktop/text/this.txt'

)as f:

img=f.read(4)

#讀取前四個字元

img1=f.read(

)#全部讀取

img2=f.readline(

)#讀取一行字元返回

img3=f.readlines(

)#文字檔案中,每一行作為乙個字串存入列表中,返回該列表

注意:最好分開乙個乙個寫,不要並列寫讀取(如上不提倡)

題目:2.實現類似dos命令中的tree命令功能

方法1:

運用root,dirs,files

import os

a=open

('d:/hzh/2.txt'

,'w'

)path=

'd:/hzh'

for root,dirs,files in os.walk(path)

:for i in root:

#root遍歷包括當前資料夾下的所有子資料夾的絕對路徑

a.write(i)

for i in dirs:

#dirs遍歷當前資料夾下的資料夾名稱(不包括當前資料夾且僅包括當前資料夾的子資料夾)

a.write(i)

a.write(

'\n'

)for i in files:

#files遍歷當前資料夾下的檔案(不包括資料夾且僅包括當前資料夾下的子檔案)

a.write(i)

a.write(

'\n'

)

注意:1.引入os模組;

2.用os.walk(path)一定需要root,dirs,files,一同迴圈。(缺一不可)

效果圖

方法2:

在當前資料夾下 (shift +滑鼠右鍵) 開啟終端輸入 tree /f > 1.txt 則將資料夾下的檔名放入1.txt檔案中。

Python學習筆記 part 4

ide vscode python版本 python3.6 學習教材 python程式設計從入門到實踐 人民郵電出版社 if語句 if語句中判斷相等用 同c 該條件測試無視大小寫。且和或分別用and和or表示。檢查特定值是否包含在列表中用 元素 in 列表名 如下,如果為真執行print true ...

閱讀 《如何高效學習》part3 part4

步驟 選擇要學習的概念 設想你是老師,正在試圖給乙個新生教這個知識點 當你感到疑惑時,返回前面,繼續理解 用簡單化的比喻來描述這個概念 每週至少要3 5次運動,每次至少40分鐘 每天7小時的睡眠 吃飯的時候吃一些有益的 健康的蔬菜 多喝水 晚上也可以吃一些東西的 不要想著學習這麼沉重的事情 其實學習...

Python學習筆記 part 2

ide vscode python版本 python3.6 學習教材 python程式設計從入門到實踐 人民郵電出版社 列表類似c中的陣列,但其中的元素可以沒有任何關係。列表用表示,用逗號分隔元素。直接列印列表將會把括號 元素 引號 逗號一併打出來,見書p31。列印列表元素不會有其他多餘符號或文字。...