Python常見檔案操作的函式示例

2021-09-02 06:12:49 字數 3447 閱讀 3436

python常見檔案操作示例

os.path 模組中的路徑名訪問函式分隔basename() 去掉目錄路徑, 返回檔名dirname() 去掉檔名, 返回目錄路徑join() 將分離的各部分組合成乙個路徑名split() 返回 (dirname(), basename()) 元組splitdrive() 返回 (drivename, pathname) 元組splitext() 返回 (filename, extension) 元組

資訊getatime() 返回最近訪問時間getctime() 返回檔案建立時間getmtime() 返回最近檔案修改時間getsize() 返回檔案大小(以位元組為單位)

查詢exists() 指定路徑(檔案或目錄)是否存在isabs() 指定路徑是否為絕對路徑isdir() 指定路徑是否存在且為乙個目錄isfile() 指定路徑是否存在且為乙個檔案islink() 指定路徑是否存在且為乙個符號鏈結ismount() 指定路徑是否存在且為乙個掛載點samefile() 兩個路徑名是否指向同個檔案

os.path.isdir(name):判斷name是不是乙個目錄,name不是目錄就返回false os.path.isfile(name):判斷name是不是乙個檔案,不存在name也返回false os.path.exists(name):判斷是否存在檔案或目錄name os.path.getsize(name):獲得檔案大小,如果name是目錄返回0l os.path.abspath(name):獲得絕對路徑os.path.normpath(path):規範path字串形式os.path.split(name):分割檔名與目錄(事實上,如果你完全使用目錄,它也會將最後乙個目錄作為檔名而分離,同時它不會判斷檔案或目錄是否存在)

os.path.splitext():分離檔名與副檔名os.path.join(path,name):連線目錄與檔名或目錄os.path.basename(path):返回檔名os.path.dirname(path):返回檔案路徑

os模組中的檔案操作:os 模組屬性linesep 用於在檔案中分隔行的字串sep 用來分隔檔案路徑名的字串pathsep 用於分隔檔案路徑的字串curdir 當前工作目錄的字串名稱pardir (當前工作目錄的)父目錄字串名稱

1.重新命名:os.rename(old, new)

2.刪除:os.remove(file)

3.列出目錄下的檔案:os.listdir(path)

4.獲取當前工作目錄:os.getcwd()

5.改變工作目錄:os.chdir(newdir)

6.建立多級目錄:os.makedirs(r"c:\python\test")

7.建立單個目錄:os.mkdir("test")

8.刪除多個目錄:os.removedirs(r"c:\python") #刪除所給路徑最後乙個目錄下所有空目錄。

9.刪除單個目錄:os.rmdir("test")

10.獲取檔案屬性:os.stat(file)

11.修改檔案許可權與時間戳:os.chmod(file)

12.執行作業系統命令:os.system("dir")

13.啟動新程序:os.exec(), os.execvp()

14.在後台執行程式:osspawnv()

15.終止當前程序:os.exit(), os._exit()

16.分離檔名:os.path.split(r"c:\python\hello.py") ——> ("c:\\python

", "hello.py")

17.分離副檔名:os.path.splitext(r"c:\python\hello.py") ——> ("c:\\python\\hello

", ".py")

18.獲取路徑名:os.path.dirname(r"c:\python\hello.py") ——> "c:\\python

"19.獲取檔名:os.path.basename(r"r:\python\hello.py") ——> "hello.py"

20.判斷檔案是否存在:os.path.exists(r"c:\python\hello.py") ——> true

21.判斷是否是絕對路徑:os.path.isabs(r".\python\") ——> false

22.判斷是否是目錄:os.path.isdir(r"c:\python") ——> true

23.判斷是否是檔案:os.path.isfile(r"c:\python\hello.py") ——> true

25.獲取檔案大小:os.path.getsize(filename)

26.*******:os.ismount("c:\\") ——> true

27.搜尋目錄下的所有檔案:os.path.walk()

shutil模組對檔案的操作:1.複製單個檔案:shultil.copy(oldfile, newfle)

2.複製整個目錄樹:shultil.copytree(r".\setup", r".\backup")

3.刪除整個目錄樹:shultil.rmtree(r".\backup")

臨時檔案的操作:1.建立乙個唯一的臨時檔案:tempfile.mktemp() ——> filename

2.開啟臨時檔案:tempfile.temporaryfile()

記憶體檔案(stringio和cstringio)操作[4.stringio] #cstringio是stringio模組的快速實現模組

1.建立記憶體檔案並寫入初始資料:f = stringio.stringio("hello world!")

2.讀入記憶體檔案資料:print f.read() #或print f.getvalue() ——> hello world!

3.想記憶體檔案寫入資料:f.write("good day!")

4.關閉記憶體檔案:f.close()

''' import os import os.path import unittest import time #import pygame

class pyfilecommonoperatortest(unittest.testcase):def __init__(self):"""constructor"""

def test01(self):print os.linesep print os.sep print os.pathsep print os.curdir print os.pardir print os.getcwd()

print 'unittest here'

if __name__ == "__main__":t = pyfilecommonoperatortest()

t.test01()

view plainprint?

view plainprint?

#讀取所有檔案內容:open('xxoo.txt')。read()

#讀取固定位元組open('abinfile', 'rb')。read(100)

#讀每行file_object.readlines()

Python常見檔案操作的函式

coding utf8 python常見檔案操作示例 os.path 模組中的路徑名訪問函式 分隔 basename 去掉目錄路徑,返回檔名 dirname 去掉檔名,返回目錄路徑 join 將分離的各部分組合成乙個路徑名 split 返回 dirname basename 元組 splitdriv...

Python常見檔案操作的函式示例

coding utf8 python常見檔案操作示例 os.path 模組中的路徑名訪問函式 分隔basename 去掉目錄路徑,返回檔名 dirname 去掉檔名,返回目錄路徑 join 將分離的各部分組合成乙個路徑名 split 返回 dirname basename 元組 splitdrive...

Python常見檔案操作的函式示例

coding utf8 python常見檔案操作示例 os.path 模組中的路徑名訪問函式 分隔basename 去掉目錄路徑,返回檔名 dirname 去掉檔名,返回目錄路徑 join 將分離的各部分組合成乙個路徑名 split 返回 dirname basename 元組 splitdrive...