零基礎(chǔ)小白必看:python基本操作-文件、目錄及路徑

使用python的os模塊,簡(jiǎn)單方便完成對(duì)文件夾、文件及路徑的管理與訪問操作。

1 前言

在最近開發(fā)中,經(jīng)常需要對(duì)文件進(jìn)行讀取、遍歷、修改等操作,想要快速、簡(jiǎn)單的完成這些操作,我選擇用 python 。通過(guò) python 的標(biāo)準(zhǔn)內(nèi)置 os 模塊,只需要幾行代碼,即可完成想要的操作。經(jīng)過(guò)對(duì) os 的使用,本文把 os 模塊的常用的操作進(jìn)行總結(jié),主要分為以下幾個(gè)劃分:

文件夾操作:即文件夾的創(chuàng)建、修改(改名/移動(dòng)),查詢(查看、遍歷)、刪除等。

文件操作:即文件的創(chuàng)建、修改、讀取、刪除等。

(文件夾/文件)路徑操作:即文件夾或文件的路徑操作,如絕對(duì)路徑,文件名與路徑分割,擴(kuò)展名分割等

本文涉及常用 的 os 函數(shù)的使用展示,主要使用 python 交互模式下進(jìn)行代碼說(shuō)明。后續(xù)操作默認(rèn)已經(jīng)引入 os 模塊,如下:

import os

復(fù)制代碼

?

如果大家在學(xué)習(xí)中遇到困難,想找一個(gè)python學(xué)習(xí)交流環(huán)境,可以加入我們的python圈,裙號(hào)930900780,可領(lǐng)取python學(xué)習(xí)資料,會(huì)節(jié)約很多時(shí)間,減少很多遇到的難題。

2 文件夾操作

以本地 E://pythontest 目錄作為演示目錄,此目錄下當(dāng)前文件如下:

test

│ test.txt

└─test-1

? ? test-1.txt

復(fù)制代碼

test 及 test-1 是文件夾,test.txt 及 test-1.txt 是文件。

2.1 查詢操作

熟悉 linux 同學(xué)應(yīng)該對(duì) ls / pwd / cd 等操作不陌生,對(duì)應(yīng)的 python 也有對(duì)應(yīng)的方法,主要包括:

listdir : 文件及目錄列表

getcwd :獲取當(dāng)前目錄

chdir :更換目錄

stat :文件及目錄基本信息

walk :遞歸遍歷目錄

>>> os.chdir("E://pythontest")? # 更改目錄

>>> os.getcwd()? ? ? ? ? ? ? ? # 獲取當(dāng)前目錄

'E:\\pythontest'

>>> os.listdir("test")? ? ? ? ? # 文件及目錄列表,相對(duì)路徑

['test-1', 'test.txt']? ? ? ? ?

>>> os.listdir("E://pythontest/test")? # 文件及目錄列表,絕對(duì)路徑

['test-1', 'test.txt']

>>> os.stat("test")? ? ? ? ? ? # 獲取目錄信息

os.stat_result(st_mode=16895, st_ino=4503599627377599, st_dev=266147611, st_nlink=1, st_uid=0, st_gid=0, st_size=0, st_atime=1590833033, st_mtime=1590832647, st_ctime=1590832207)

>>> os.stat("test/test.txt")? ? # 獲取文件信息

os.stat_result(st_mode=33206, st_ino=2251799813692354, st_dev=266147611, st_nlink=1, st_uid=0, st_gid=0, st_size=4, st_atime=1590832653, st_mtime=1590832609, st_ctime=1590832598)

復(fù)制代碼

其中 stat 函數(shù)返回的是文件或者目錄的基本信息,具體如下:

st_mode:inode 保護(hù)模式

st_ino:inode 節(jié)點(diǎn)號(hào)。

st_dev:inode 駐留的設(shè)備。

st_nlink:inode 的鏈接數(shù)。

st_uid:所有者的用戶ID。

st_gid:所有者的組ID。

st_size:普通文件以字節(jié)為單位的大小

st_atime:上次訪問的時(shí)間。

st_mtime:最后一次修改的時(shí)間。

st_ctime:創(chuàng)建時(shí)間。

日常使用中,我們一般使用 st_size 、st_ctime 及 st_mtime 獲取文件大小,創(chuàng)建時(shí)間,修改時(shí)間。另外,我們看到輸出的時(shí)間是秒數(shù),在這里提一下,關(guān)于日期的轉(zhuǎn)換處理。

(1)秒數(shù)轉(zhuǎn)日期時(shí)間格式字符串

>>> import time? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 引入time模塊

>>> timestruct = time.localtime(1590803070)? # 轉(zhuǎn)換為時(shí)間結(jié)構(gòu)體

>>> print(timestruct)

time.struct_time(tm_year=2020, tm_mon=5, tm_mday=30, tm_hour=9, tm_min=44, tm_sec=30, tm_wday=5, tm_yday=151, tm_isdst=0)

>>> time.strftime("%Y-%m-%d %H:%M:%S",timestruct)? # 格式化時(shí)間

'2020-05-30 09:44:30'

復(fù)制代碼

(2)格式日期時(shí)間字符串轉(zhuǎn)秒數(shù)

>>> import datetime? ? ? ? ? ? ? # 引入datetime模塊

>>> timeobject = datetime.datetime.strptime("2020-05-23 10:00:00","%Y-%m-%d %H:%M:%S") #解析時(shí)間字符串為時(shí)間對(duì)象

>>> timeseconds=time.mktime(timeobject.timetuple())? # 獲取時(shí)間秒數(shù)

>>> print(int(timeseconds))? ? ? # 轉(zhuǎn)為int顯示

1590199200

復(fù)制代碼

遍歷操作 walk 函數(shù)對(duì)目錄進(jìn)行遞歸遍歷,返回 root,dirs,files,分別對(duì)應(yīng)當(dāng)前的遍歷的目錄,此目錄中的子目錄及文件。

>>> data = os.walk("test")? ? ? ? ? ? ? # 遍歷test目錄

>>> for root,dirs,files in data:? ? ? ? # 遞歸遍歷及輸出

...? ? print("root:%s" % root)

...? ? for dir in dirs:

...? ? ? print(os.path.join(root,dir))

...? ? for file in files:

...? ? ? print(os.path.join(root,file))

...

root:test

test\test-1

test\test-2

test\test.txt

root:test\test-1

test\test-1\test-1.txt

root:test\test-2

test\test-2\test-2.txt

復(fù)制代碼

2.2 創(chuàng)建操作

mkdir :新建單個(gè)目錄,若目錄路徑中父目錄不存在,則創(chuàng)建失敗

makedirs :新建多個(gè)目錄,若目錄路徑中父目錄不存在,則自動(dòng)創(chuàng)建

>>> os.mkdir("test")

>>> os.mkdir("test1/test1-1")? ? ? ? ? # 父目錄不存在,報(bào)錯(cuò)

Traceback (most recent call last):

? File "<stdin>", line 1, in <module>

FileNotFoundError: [WinError 3] 系統(tǒng)找不到指定的路徑。: 'test1/test1-1'

>>> os.makedirs("test1/test1-1")? ? ? # 父目錄不存在,自動(dòng)創(chuàng)建

>>> os.listdir("test1")

['test1-1']

復(fù)制代碼

2.3 刪除操作

rmdir :刪除單個(gè)空目錄,目錄不為空則報(bào)錯(cuò)

removedirs : 按路徑刪除遞歸多級(jí)空目錄,目錄不為空則報(bào)錯(cuò)

>>> os.rmdir("test1")? ? ? ? ? ? ? ? ? ? ? ? # 若目錄不為空,報(bào)錯(cuò)

Traceback (most recent call last):

? File "<stdin>", line 1, in <module>

OSError: [WinError 145] 目錄不是空的。: 'test1'

>>> os.rmdir("test1/test1-1")

>>> os.removedirs("test1/test1-1")? ? ? ? ? ? # 刪除多級(jí)空目錄

>>> os.listdir(".")

['test']

復(fù)制代碼

由于刪除空目錄的限制,更多的是使用 shutil 模塊中的 rmtree 函數(shù),可以刪除不為空的目錄及其文件。

2.4 修改操作

rename :重命名目錄或文件,可修改文件或目錄的路徑(即移動(dòng)操作),若目標(biāo)文件目錄不存在,則報(bào)錯(cuò)。

renames :重命名目錄或文件,若目標(biāo)文件目錄不存在,則自動(dòng)創(chuàng)建

>>> os.makedirs("test1/test1-1")

>>> os.rename("test1/test1-1","test1/test1-2")? ? # test1-1 修改為test1-2

>>> os.listdir("test1")

['test1-2']

>>> os.rename("test1/test1-2","test2/test2-2")? ? # 由于test2目錄不存在,報(bào)錯(cuò)

Traceback (most recent call last):

? File "<stdin>", line 1, in <module>

FileNotFoundError: [WinError 3] 系統(tǒng)找不到指定的路徑。: 'test1/test1-2' -> 'test2/test2-2'

>>> os.renames("test1/test1-2","test2/test2-2")? ? # renames可自動(dòng)創(chuàng)建不存在的目錄

>>> os.listdir("test2")

['test2-2']

復(fù)制代碼

如果目標(biāo)路徑文件已經(jīng)存在,那么os.rename()和os.renames()都會(huì)報(bào)錯(cuò):FileExistsError: [WinError 183] 當(dāng)文件已存在時(shí),無(wú)法創(chuàng)建該文件。

3 文件操作

3.1 查詢操作

open/read/close :文件讀取

stat :文件信息,詳細(xì)見前面文件夾中的 stat 說(shuō)明

>>> f = os.open("test/test.txt", os.O_RDWR|os.O_CREAT)? # 打開文件

>>> str_bytes = os.read(f,100)? ? ? ? ? ? ? ? ? ? ? ? ? # 讀100字節(jié)

>>> str = bytes.decode(str_bytes)? ? ? ? ? ? ? ? ? ? ? # 字節(jié)轉(zhuǎn)字符串

>>> print(str)

test write data

>>> os.close(f)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 關(guān)閉文件

復(fù)制代碼

注意 open/read/close 需要一起操作,其中 open 操作需要指定模式,上述是以讀寫模式打開文件,若文件不存在則創(chuàng)建文件。各模式具體如下:

flags-- 該參數(shù)可以是以下選項(xiàng),多個(gè)使用 "|" 隔開:

os.O_RDONLY:以只讀的方式打開

os.O_WRONLY:以只寫的方式打開

os.O_RDWR :以讀寫的方式打開

os.O_NONBLOCK:打開時(shí)不阻塞

os.O_APPEND:以追加的方式打開

os.O_CREAT:創(chuàng)建并打開一個(gè)新文件

os.O_TRUNC:打開一個(gè)文件并截?cái)嗨拈L(zhǎng)度為零(必須有寫權(quán)限)

os.O_EXCL:如果指定的文件存在,返回錯(cuò)誤

os.O_SHLOCK:自動(dòng)獲取共享鎖

os.O_EXLOCK:自動(dòng)獲取獨(dú)立鎖

os.O_DIRECT:消除或減少緩存效果

os.O_FSYNC :同步寫入

os.O_NOFOLLOW:不追蹤軟鏈接

3.2 創(chuàng)建操作

前面已提到,使用 open ,指定模式, 若文件不存在,則創(chuàng)建。有點(diǎn)類似 linux 操作中的 touch。

>>> f = os.open("test/test.txt", os.O_RDWR|os.O_CREAT)? # 若文件不存在,則創(chuàng)建

>>> os.close(f)

復(fù)制代碼

3.3 修改操作

open/write/close :寫入文件內(nèi)容

rename ,renames : 與前面介紹的修改名稱、移動(dòng)操作一致。

>>> f = os.open("test/test.txt", os.O_RDWR|os.O_CREAT)? ? # 打開文件

>>> os.write(f,b"test write data")? ? ? ? ? ? ? ? ? ? ? ? # 寫入內(nèi)容

15

>>> os.close(f)? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? ? # 關(guān)閉文件

復(fù)制代碼

3.4 刪除

remove :刪除文件,注意不能刪除目錄(使用 rmdir/removedirs)

>>> os.remove("test/test-1")? ? ? # 刪除目錄報(bào)錯(cuò)

Traceback (most recent call last):

? File "<stdin>", line 1, in <module>

FileNotFoundError: [WinError 2] 系統(tǒng)找不到指定的文件。: 'test/test1'

>>> os.remove("test/test.txt")? ? # 刪除文件

>>> os.listdir("test")

['test-1']

復(fù)制代碼

4 路徑操作

在使用文件或目錄過(guò)程中,經(jīng)常需要對(duì)文件及目錄路徑進(jìn)行處理,因此,os 中有一個(gè)子模塊 path,專門就是處理路徑操作的。主要有以下操作:

abspath :返回絕對(duì)路徑

>>> os.path.abspath("test")

'E:\\pythontest\\test'

復(fù)制代碼

exists :判斷文件或目錄是否存在

>>> os.path.exists("test")

True

>>> os.path.exists("test/test.txt")

False

>>> os.path.exists("test/test-1/test-1.txt")

True

復(fù)制代碼

isfile/isdir :判斷是否為文件/目錄

>>> os.path.isdir("test")

True

>>> os.path.isfile("test/test-1/test-1.txt")

True

復(fù)制代碼

basename/dirname:獲取路徑尾部和路徑頭部。其實(shí)就是以路徑中最后一個(gè) / 為分割符,分為頭(head) 和尾(tail)兩部分,tail 是 basename 返回的內(nèi)容,head 是 dirname 返回的內(nèi)容。經(jīng)常用于獲取文件名,目錄名等操作

>>> os.path.basename("test/test-1/test-1.txt")? # 文件名

'test-1.txt'

>>> os.path.basename("test/test-1/")? ? # 空內(nèi)容

''

>>> os.path.basename("test/test-1")? ? ? # 目錄名

'test-1'

>>> os.path.dirname("test/test-1/test-1.txt")? # 文件所在目錄路徑

'test/test-1'

>>> os.path.dirname("test/test-1/")? # 目錄路徑

'test/test-1'

>>> os.path.dirname("test/test-1")? # 父目錄路徑

'test'

復(fù)制代碼

join :合成路徑,即把兩個(gè)參數(shù)使用系統(tǒng)路徑分割符進(jìn)行連接,形成完整路徑。

>>> os.path.join("test","test-1")? # 連接兩個(gè)目錄

'test\\test-1'

>>> os.path.join("test\\test-1","test-1.txt")? # 連接目錄與文件名

'test\\test-1\\test-1.txt'

復(fù)制代碼

split :分割文件名和文件夾,即把 path 以最后一個(gè)斜線"/"為分隔符,切割為 head 和 tail ,以 (head, tail) 元組的形勢(shì)返回。

>>> os.path.split("test/test-1")? ? # 分割目錄

('test', 'test-1')

>>> os.path.split("test/test-1/")? ? # 以/結(jié)尾的目錄分割

('test/test-1', '')

>>> os.path.split("test/test-1/test-1.txt")? # 分割文件

('test/test-1', 'test-1.txt')

復(fù)制代碼

splitext :分割路徑名和文件擴(kuò)展名,把path 以最后一個(gè)擴(kuò)展名分隔符“.”分割,切割為 head 和 tail ,以 (head, tail) 元組的形勢(shì)返回。注意與 split 的區(qū)別是分隔符的不同。

>>> os.path.splitext("test/test-1")?

('test/test-1', '')

>>> os.path.splitext("test/test-1/")

('test/test-1/', '')

>>> os.path.splitext("test/test-1/test-1.txt")? # 區(qū)分文件名及擴(kuò)展名

('test/test-1/test-1', '.txt')

>>> os.path.splitext("test/test-1/test-1.txt.tmp") # 以最后的"."為分割點(diǎn)

('test/test-1/test-1.txt', '.tmp')

復(fù)制代碼

5 示例應(yīng)用

下面以一些平時(shí)使用到的場(chǎng)景,對(duì)前面的操作函數(shù)進(jìn)行綜合使用。

5.1 批量修改文件名

def batch_rename(dir_path):

? ? itemlist = os.listdir(dir_path)

? ? # 獲取目錄文件列表

? ? for item in itemlist:

? ? ? ? # 連接成完整路徑

? ? ? ? item_path = os.path.join(dir_path, item)

? ? ? ? print(item_path)

? ? ? ? # 修改文件名

? ? ? ? if os.path.isfile(item_path):

? ? ? ? ? ? splitext = os.path.splitext(item_path)

? ? ? ? ? ? os.rename(item_path, splitext[0] + "-副本" + splitext[1])

復(fù)制代碼

?

5.2 遍歷目錄及子目錄下所有指定擴(kuò)展名的文件

def walk_ext_file(dir_path,ext):

? ? # 遍歷

? ? for root, dirs, files in os.walk(dir_path):

? ? ? ? # 獲取文件名稱及路徑

? ? ? ? for file in files:

? ? ? ? ? ? file_path = os.path.join(root, file)

? ? ? ? ? ? file_item = os.path.splitext(file_path)

? ? ? ? ? ? # 輸出指定擴(kuò)展名的文件路徑

? ? ? ? ? ? if ext == file_item[1]:

? ? ? ? ? ? ? ? print(file_path)

復(fù)制代碼

?

5.3 按修改時(shí)間排序指定目錄下的文件

def sort_file(dir_path):

? ? # 排序前

? ? itemlist = os.listdir(dir_path)

? ? print(itemlist)

? ? # 正向排序

? ? itemlist.sort(key=lambda filename: os.path.getmtime(os.path.join(dir_path, filename)))

? ? print(itemlist)

? ? # 反向排序

? ? itemlist.sort(key=lambda filename: os.path.getmtime(os.path.join(dir_path, filename)), reverse=True)

? ? print(itemlist)

? ? # 獲取最新修改的文件

? ? print(itemlist[0])

復(fù)制代碼

?

6 總結(jié)

在需要對(duì)文件或者目錄進(jìn)行操作時(shí),python 是一個(gè)簡(jiǎn)單快速選擇。本文通過(guò) python 的標(biāo)準(zhǔn)內(nèi)置 os 模塊及子模塊 os.path 的常用方法進(jìn)行介紹,最后結(jié)合使用場(chǎng)景進(jìn)行綜合使用。相信已經(jīng)滿足大家對(duì)文件及目錄操作的大部分需求。

最后多說(shuō)一句,小編是一名python開發(fā)工程師,這里有我自己整理了一套最新的python系統(tǒng)學(xué)習(xí)教程,包括從基礎(chǔ)的python腳本到web開發(fā)、爬蟲、數(shù)據(jù)分析、數(shù)據(jù)可視化、機(jī)器學(xué)習(xí)等。想要這些資料的可以進(jìn)裙930900780領(lǐng)取。

本文章素材來(lái)源于網(wǎng)絡(luò),如有侵權(quán)請(qǐng)聯(lián)系刪除。

?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

友情鏈接更多精彩內(nèi)容