1. 程式人生 > >第030講:檔案系統:介紹一個高大上的東西

第030講:檔案系統:介紹一個高大上的東西

目錄

動動手

0. 請寫下這一節課你學習到的內容:格式不限,回憶並複述是加強記憶的好方式!

介紹跟Python的檔案相關且十分有用的模組。模組是什麼?
模組是一個包含你定義的函式和變數的檔案,字尾是.py,模組可以做到被別的程式所引入,以使用該模組中的函式等功能。

OS模組(Operating System作業系統)

對於檔案系統的訪問來說,Python一般是提供OS模組來實現就可以了,我們所知道常用的作業系統有:Windows,Mac OS,Linux,UNIX等,這些作業系統底層由於檔案系統的訪問工作原理不同,因此你可能就要針對不同的系統來考慮使用哪些檔案系統模組....這樣的做法是非常不友好且麻煩的,因為這樣就意味著當你的程式執行環境一改變,你就要相應的去修改大量的程式碼來應付。但是我們的Python是跨平臺的,所以Python就有了這個OS模組。

有了OS模組,我們不需要關心什麼作業系統下使用什麼模組,OS模組會幫你選擇正確的模組並呼叫。

os模組中關於檔案/目錄常用的函式使用方法

函式名

使用方法

getcwd() 返回當前工作目錄
chdir(path) 改變工作目錄
listdir(path='.') 列舉指定目錄中的檔名('.'表示當前目錄,'..'表示上一級目錄)
mkdir(path) 建立單層目錄,如該目錄已存在丟擲異常
makedirs(path) 遞迴建立多層目錄,如該目錄已存在丟擲異常,注意:'E:\\a\\b'和'E:\\a\\c'並不會衝突
remove(path) 刪除檔案
rmdir(path) 刪除單層目錄,如該目錄非空則丟擲異常
removedirs(path) 遞迴刪除目錄,從子目錄到父目錄逐層嘗試刪除,遇到目錄非空則丟擲異常
rename(old, new) 將檔案old重新命名為new
system(command) 執行系統的shell命令
walk(top) 遍歷top路徑以下所有的子目錄,返回一個三元組:(路徑, [包含目錄], [包含檔案])【具體實現方案請看:第30講課後作業^_^】

以下是支援路徑操作中常用到的一些定義,支援所有平臺

os.curdir 指代當前目錄('.')
os.pardir 指代上一級目錄('..')
os.sep 輸出作業系統特定的路徑分隔符(Win下為'\\',Linux下為'/')
os.linesep 當前平臺使用的行終止符(Win下為'\r\n',Linux下為'\n')
os.name 指代當前使用的作業系統(包括:'posix',  'nt', 'mac', 'os2', 'ce', 'java')

os.path模組中關於路徑常用的函式使用方法

函式名

使用方法

basename(path) 去掉目錄路徑,單獨返回檔名
dirname(path) 去掉檔名,單獨返回目錄路徑
join(path1[, path2[, ...]]) 將path1, path2各部分組合成一個路徑名
split(path) 分割檔名與路徑,返回(f_path, f_name)元組。如果完全使用目錄,它也會將最後一個目錄作為檔名分離,且不會判斷檔案或者目錄是否存在
splitext(path) 分離檔名與副檔名,返回(f_name, f_extension)元組
getsize(file) 返回指定檔案的尺寸,單位是位元組
getatime(file) 返回指定檔案最近的訪問時間(浮點型秒數,可用time模組的gmtime()或localtime()函式換算)
getctime(file) 返回指定檔案的建立時間(浮點型秒數,可用time模組的gmtime()或localtime()函式換算)
getmtime(file) 返回指定檔案最新的修改時間(浮點型秒數,可用time模組的gmtime()或localtime()函式換算)

以下為函式返回 True 或 False

exists(path) 判斷指定路徑(目錄或檔案)是否存在
isabs(path) 判斷指定路徑是否為絕對路徑
isdir(path) 判斷指定路徑是否存在且是一個目錄
isfile(path) 判斷指定路徑是否存在且是一個檔案
islink(path) 判斷指定路徑是否存在且是一個符號連結(在windows上即為快捷方式)
ismount(path) 判斷指定路徑是否存在且是一個掛載點
samefile(path1, paht2) 判斷path1和path2兩個路徑是否指向同一個檔案

動動手

0. 編寫一個程式,統計當前目錄下每個檔案型別的檔案數,程式實現如圖:

import os

all_files = os.listdir(os.curdir) # 使用os.curdir表示當前目錄更標準
type_dict = dict()

for each_file in all_files:
    if os.path.isdir(each_file):
        type_dict.setdefault('資料夾', 0)
        type_dict['資料夾'] += 1
    else:
        ext = os.path.splitext(each_file)[1]
        type_dict.setdefault(ext, 0)
        type_dict[ext] += 1

for each_type in type_dict.keys():
    print('該資料夾下共有型別為【%s】的檔案 %d 個' % (each_type, type_dict[each_type]))

2. 編寫一個程式,使用者輸入檔名以及開始搜尋的路徑,搜尋該檔案是否存在。如遇到資料夾,則進入資料夾繼續搜尋,程式實現如圖:

import os

all_files = os.listdir(os.curdir) # 使用os.curdir表示當前目錄更標準
file_dict = dict()

for each_file in all_files:
    if os.path.isfile(each_file):
        file_size = os.path.getsize(each_file)
        file_dict[each_file] = file_size

for each in file_dict.items():
    print('%s【%dBytes】' % (each[0], each[1]))

2. 編寫一個程式,使用者輸入檔名以及開始搜尋的路徑,搜尋該檔案是否存在。如遇到資料夾,則進入資料夾繼續搜尋,程式實現如圖:

import os

def search_file(start_dir, target) :
    os.chdir(start_dir)
    
    for each_file in os.listdir(os.curdir) :
        if each_file == target :
            print(os.getcwd() + os.sep + each_file) # 使用os.sep是程式更標準
        if os.path.isdir(each_file) :
            search_file(each_file, target) # 遞迴呼叫
            os.chdir(os.pardir) # 遞迴呼叫後切記返回上一層目錄

start_dir = input('請輸入待查詢的初始目錄:')
target = input('請輸入需要查詢的目標檔案:')
search_file(start_dir, target)

3. 編寫一個程式,使用者輸入開始搜尋的路徑,查詢該路徑下(包含子資料夾內)所有的視訊格式檔案(要求查詢mp4 rmvb, avi的格式即可),並把建立一個檔案(vedioList.txt)存放所有找到的檔案的路徑,程式實現如圖:

import os

def search_file(start_dir, target) :
    os.chdir(start_dir)
    
    for each_file in os.listdir(os.curdir) :
        ext = os.path.splitext(each_file)[1]
        if ext in target :
            vedio_list.append(os.getcwd() + os.sep + each_file + os.linesep) # 使用os.sep是程式更標準
        if os.path.isdir(each_file) :
            search_file(each_file, target) # 遞迴呼叫
            os.chdir(os.pardir) # 遞迴呼叫後切記返回上一層目錄

start_dir = input('請輸入待查詢的初始目錄:')
program_dir = os.getcwd()

target = ['.mp4', '.avi', '.rmvb']
vedio_list = []

search_file(start_dir, target)

f = open(program_dir + os.sep + 'vedioList.txt', 'w')
f.writelines(vedio_list)
f.close()

4. 編寫一個程式,使用者輸入關鍵字,查詢當前資料夾內(如果當前資料夾內包含資料夾,則進入資料夾繼續搜尋)所有含有該關鍵字的文字檔案(.txt字尾),要求顯示該檔案所在的位置以及關鍵字在檔案中的具體位置(第幾行第幾個字元),程式實現如圖:

import os

def print_pos(key_dict):
    keys = key_dict.keys()
    keys = sorted(keys) # 由於字典是無序的,我們這裡對行數進行排序
    for each_key in keys:
        print('關鍵字出現在第 %s 行,第 %s 個位置。' % (each_key, str(key_dict[each_key])))


def pos_in_line(line, key):
    pos = []
    begin = line.find(key)
    while begin != -1:
        pos.append(begin + 1) # 使用者的角度是從1開始數
        begin = line.find(key, begin+1) # 從下一個位置繼續查詢

    return pos


def search_in_file(file_name, key):
    f = open(file_name)
    count = 0 # 記錄行數
    key_dict = dict() # 字典,使用者存放key所在具體行數對應具體位置
    
    for each_line in f:
        count += 1
        if key in each_line:
            pos = pos_in_line(each_line, key) # key在每行對應的位置
            key_dict[count] = pos
    
    f.close()
    return key_dict


def search_files(key, detail):    
    all_files = os.walk(os.getcwd())
    txt_files = []

    for i in all_files:
        for each_file in i[2]:
            if os.path.splitext(each_file)[1] == '.txt': # 根據字尾判斷是否文字檔案
                each_file = os.path.join(i[0], each_file)
                txt_files.append(each_file)

    for each_txt_file in txt_files:
        key_dict = search_in_file(each_txt_file, key)
        if key_dict:
            print('================================================================')
            print('在檔案【%s】中找到關鍵字【%s】' % (each_txt_file, key))
            if detail in ['YES', 'Yes', 'yes']:
                print_pos(key_dict)


key = input('請將該指令碼放於待查詢的資料夾內,請輸入關鍵字:')
detail = input('請問是否需要列印關鍵字【%s】在檔案中的具體位置(YES/NO):' % key)
search_files(key, detail)

vczXAsdU~o^=21'* ![W>y`hi;V+