python怎麼刪除檔案內容(Python建立資料夾)

1. 介紹

1.1 介紹

今天福哥帶著大家來學習Python讀寫檔案的方法,雖然現階段做專案需要用到檔案操作的情況不多了,但是免不了在特殊情況下還是需要用到這個技術的。

今天福哥還會給大家講解Python建立、刪除、授權資料夾的方法,這個技術在控制檯程式裡面用途是非常多的。

Python建立、刪除、授權資料夾以及讀寫檔案是依靠os庫來實現的,而檔案路徑則是通過os的path屬性物件的方法來處理的,我們來逐個介紹一下。

資料夾操作就是目錄操作,在Windows系統裡面資料夾叫folder,翻譯過來就是資料夾,在Linux系統裡面資料夾叫directory,翻譯過來就是目錄。所以建立、刪除、授權資料夾就是建立、刪除、授權目錄。

2. 基本原則

讀寫檔案有一些常識需要大家先了解一下。

  • 讀寫檔案可以是本地電腦上面的檔案,也可以是遠端網路上面的檔案,只要授權了就可以操作。
  • 資料夾操作可以是本地電腦上面的資料夾,也可以是遠端網路上面的資料夾,只要授權了就可以操作。
  • 要建立檔案需要對建立檔案的資料夾有寫許可權。
  • 讀寫已經存在的檔案只需要對檔案有許可權。
  • 檔案內容分為普通模式和二進位制模式,普通模式通過字串操作,二進位制模式通過位元組操作。
  • 寫檔案分為重置寫入和追加寫入,前者會清空已有內容,後者不會。
  • 通過檔案指標可以精確控制讀寫檔案內容的具體位置,但是寫入只會覆蓋已有內容而不會像編輯器一樣插入內容。
  • 當前資料夾通過“.”表示,上一級資料夾通過“..”表示。
  • 任何資料夾都會有“當前資料夾”和“上一級資料夾”。

3. 資料夾

3.1 遞迴遍歷

遍歷資料夾用到os庫的walk方法,這個方法很方便,直接就把所有的子級、孫級的全部資料夾和檔案都遍歷出來了。

rootDir = os.path.dirname(os.path.abspath(__file__))   "//TFSE"for root,dirs,files in os.walk(rootDir):    for dir in dirs:        print("資料夾:"   os.path.abspath(root   "//"   dir))    for file in files:        print("檔案:"   os.path.abspath(root   "//"   file))1234567

3.2 不遞迴遍歷

os的walk方法很簡單的解決了遍歷資料夾的問題,但是並不是任何情況下我們都需要把資料夾裡面的子級、孫級全部都找出來的,這時候就用到了os的另外一個方法listdir了。

通過os的listdir可以像PHP那樣只遍歷一層目錄下面的內容,可以通過os.path的isdir、isfile方法判斷路徑是資料夾還是檔案。

rootDir = os.path.dirname(os.path.abspath(__file__))   "//TFSE"files = os.listdir(rootDir)for fi in files:    fullPath = os.path.abspath(rootDir   "//"   fi)    if os.path.isfile(fullPath):        print("檔案:"   fullPath)    elif os.path.isdir(fullPath):        print("資料夾:"   fullPath)123456789

3.3 普通遍歷

既然os的listdir可以遍歷一層目錄,通過遞迴函式也就可以遍歷所有子級、孫級的目錄了。方法和上一課的PHP一樣,封裝一個函式,在函式裡面呼叫函式自己,實現遞迴。

rootDir = os.path.dirname(os.path.abspath(__file__))   "//TFSE"def listFolder(dir):    files = os.listdir(dir)    for fi in files:        fullPath = os.path.abspath(dir   "//"   fi)        if os.path.isfile(fullPath):            print("檔案:"   fullPath)        elif os.path.isdir(fullPath):            print("資料夾:"   fullPath)            listFolder(fullPath)listFolder(rootDir)12345678910111213

3.4 建立

建立資料夾使用os的mkdir方法,建立之後可以通過os的chmod方法授權,通過os的chown方法設定角色。如果建立失敗(包括資料夾已經存在的情況)則會丟擲異常。

需要注意的是os.chmod和os.chown只有在Linux作業系統裡面才能使用。

rootDir = os.path.dirname(os.path.abspath(__file__))   "//TFSE"newDir = rootDir   "//newFolder"os.mkdir(newDir)os.chmod(newDir, 0755)os.chown(newDir, 100, -1)123456

3.5 刪除

刪除資料夾使用os的rmdir方法。

rootDir = os.path.dirname(os.path.abspath(__file__))   "//TFSE"newDir = rootDir   "//newFolder"os.rmdir(newDir)1234

3.6 遞迴刪除

如果資料夾含有子級檔案/資料夾則需要先刪除子級專案才能刪除資料夾,這個時候就體現出os的walk方法的便捷了。

rootDir = os.path.dirname(os.path.abspath(__file__))   "//TFSE"newDir = rootDir   "//newFolder"for root,dirs,files in os.walk(newDir):    for dir in dirs:        os.rmdir(root   "//"   dir)    for file in files:        os.unlink(root   "//"   file)12345678

4. 檔案

4.1 建立

Python建立檔案只有標準方式一種方法,就是通過open、close實現的。

4.1.1 open

使用open建立檔案需要了解更多知識,首先我們需要指定“檔案開啟方式”這個概念,使用open實際上知識開啟了一個檔案,需要我們指定一個開啟檔案想要幹什麼的模式,這個就是檔案開啟方式。

檔案開啟方式包括:

  • r,讀模式
  • r ,可讀可寫模式,檔案指標在檔案開頭
  • w,寫模式,重置檔案內容
  • w ,可讀可寫模式,重置檔案內容
  • a,寫模式,檔案指標在檔案末尾
  • a ,可讀可寫模式,檔案指標在檔案末尾

rootDir = os.path.dirname(os.path.abspath(__file__))   "//TFSE"newFile = rootDir   "//newFile.py"fo = open(newFile, "w")if fo:    writeLen = fo.write("這是福哥新建立的檔案")    fo.close()1234567

4.2 刪除

刪除檔案使用os的unlink方法。

rootDir = os.path.dirname(os.path.abspath(__file__))   "//TFSE"newFile = rootDir   "//newFile.py"os.unlink(newFile)1234

4.3 讀檔案

讀檔案就是以讀模式或者讀寫模式開啟檔案。

4.3.1 open

使用open讀檔案需要用到while語句通過readline迴圈讀取。

rootDir = os.path.dirname(os.path.abspath(__file__))   "//TFSE"newFile = rootDir   "//newFile.py"fc = ""fo = open(newFile, "r")if fo:    while True:        line = fo.readline()        if not line:            break        fc  = line    fo.close()123456789101112

4.4 寫檔案

寫檔案就是以寫模式或者讀寫模式開啟檔案。

4.4.1 open

使用open寫檔案需要用到write方法。

rootDir = os.path.dirname(os.path.abspath(__file__))   "//TFSE"newFile = rootDir   "//newFile.py"fo = open(newFile, "w")if fo:    fo.write("福哥說這是全部內容了")    fo.close()fc = ""fo = open(newFile, "r")if fo:    while True:        line = fo.readline()        if not line:            break        fc  = line    fo.close()print(fc)12345678910111213141516171819

4.5 追加寫檔案

有時候我們不想將檔案內容全部重置了,需要在檔案現有內容後面追加內容怎麼辦?

4.5.1 open

將檔案開啟方式設定為追加寫模式。

rootDir = os.path.dirname(os.path.abspath(__file__))   "//TFSE"newFile = rootDir   "//newFile.py"fo = open(newFile, "a")if fo:    fo.write("福哥又說話了")    fo.close()fc = ""fo = open(newFile, "r")if fo:    while True:        line = fo.readline()        if not line:            break        fc  = line    fo.close()print(fc)12345678910111213141516171819

4.6 檔案指標

我們以可讀可寫模式開啟檔案後,通過seek移動檔案指標,在指定位置進行讀寫操作,這種方式可以避免將檔案全部內容載入到記憶體當中就可以完成很多情況的讀寫操作,可以解決針對超大檔案內容的格式化資料的編輯的難題。

4.6.1 檔案資料庫

舉例:我們製作一個檔案資料庫,將使用者名稱和密碼儲存到一個檔案裡面,使用者名稱和密碼格式如下

[使用者名稱(最長50個字元)][密碼(取MD5雜湊串,所以固定32個字元)][換行]1

然後我們只要保證每一組使用者資訊都是一樣的固定長度就可以通過簡單計算知道某一個使用者ID對應的使用者資訊在檔案的哪個位置上面,進而通過fseek就可以快速定位並讀取使用者資訊了。

因為我們規定了檔案內的使用者資料格式,那麼通過操控檔案指標就可以實現這個文字資料庫的增、刪、改、查功能了,是不是覺得很神奇?

4.6.2 文字資料庫定義

這是文字資料庫TFSimpleTextDb的定義。

class TFSimpleTextDb:    rootDir = ""    fp = ""    fo = None    fs = 0    lastUserId = 0    lastLoginUserId = 0    def __init__(self, rootDir):        self.rootDir = rootDir        self.fp = ""        self.fo = None        self.fs = 0        self.lastUserId = 0        self.lastLoginUserId = 0    def strPad(self, s, padLen, chr):        strLen = len(s)        padLen -= strLen        for i in range(0, padLen):            s  = chr        return s    def strRepeat(self, chr, num):        s = ""        for i in range(0, num):            s  = chr        return s    def conn(self):        self.fp = self.rootDir   "/users.tongfu.net.txt"        # 資料庫檔案不存在就建立它        if not os.path.exists(self.fp):            with open(self.fp, "w") as fo:                fo.close()        try:            # 開啟資料庫檔案            self.fo = open(self.fp, "r ")            # 記得當前檔案尺寸            self.fs = os.path.getsize(self.fp)            return True        except Exception as e:            print(e)        return False    def add(self, user, pwd):        if len(user) > 50:            return false        # 移動檔案指標到檔案末尾        self.fo.seek(self.fs)        # 寫入一行資料儲存使用者名稱和密碼        # 使用者名稱用strpad補位,保證資料結構一致        line = "%s%s/n" % (            self.strPad(user, 50, ' '),            md5(pwd).hexdigest()        )        self.fo.write(line)        self.fs  = len(line)        # 立即寫入新內容到磁碟        self.fo.flush()        # 新使用者ID就是最新一行的行號        self.lastUserId = int(self.fs/(50 32 1))        return True    def mod(self, userId, pwd):        # 移動檔案指標到指定userId對應的使用者資訊位置        self.fo.seek((50 32 1)*(userId-1))        # 按約定資料長度讀取資料欄位上的資料        userInDb = (self.fo.read(50)).strip()        pwdInDb = self.fo.read(32)        if userInDb == "":            return False        # 修改密碼        # 後退32個字元,在密碼欄位開始處開始寫        self.fo.seek(self.fo.tell()-32)        self.fo.write(md5(pwd).hexdigest())        # 立即寫入新內容到磁碟        self.fo.flush()        return True    def delete(self, userId):        # 移動檔案指標到指定userId對應的使用者資訊位置        self.fo.seek((50 32 1)*(userId-1))        # 按約定資料長度讀取資料欄位上的資料        userInDb = (self.fo.read(50)).strip()        pwdInDb = self.fo.read(32)        if userInDb == "":            return False        # 修改密碼        # 後退82個字元,在使用者名稱欄位開始處開始寫        # 寫入82個空字元表示使用者已經被刪除了        self.fo.seek(self.fo.tell()-82)        self.fo.write(self.strRepeat(" ", 82))        # 立即寫入新內容到磁碟        self.fo.flush()        return True    def login(self, user, pwd=""):        fo = open(self.fp, "r")        if fo:            while True:                # 解析出使用者名稱和密碼                userInDb = fo.read(50)                if not userInDb:                    break                pwdInDb = fo.read(32)                if not pwdInDb:                    break                LF = fo.read(1)                if not LF:                    break                userInDb = userInDb.strip()                # 驗證使用者名稱                if user == userInDb:                    # 驗證密碼                    if md5(pwd).hexdigest() == pwdInDb:                        # 計算登入使用者ID                        self.lastLoginUserId = int(fo.tell()/(50 32 1))                        return 0                    else:                        return 1 # 密碼錯誤            fo.close()        return 2 # 使用者名稱不存在    def close(self):        self.fo.close()    def getLastUserId(self):        return self.lastUserId    def getLastLoginUserId(self):        return self.lastLoginUserId123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161

4.6.3 文字資料庫測試

下面是對這個TFSimpleTextDb的測試程式碼。

myTFSimpleTextDb = TFSimpleTextDb(rootDir)myTFSimpleTextDb.conn()if myTFSimpleTextDb.login("福哥") == 2:    print ("建立使用者福哥/n")    myTFSimpleTextDb.add("福哥", "123456")    print (myTFSimpleTextDb.getLastUserId())if myTFSimpleTextDb.login("鬼谷子叔叔") == 2:    print ("建立使用者鬼谷子叔叔/n")    myTFSimpleTextDb.add("鬼谷子叔叔", "abcdef")    print (myTFSimpleTextDb.getLastUserId())if myTFSimpleTextDb.login("同福大哥") == 2:    print ("建立使用者同福大哥/n")    myTFSimpleTextDb.add("同福大哥", "123456")    print (myTFSimpleTextDb.getLastUserId())print ("修改鬼谷子叔叔的密碼/n")print (myTFSimpleTextDb.mod(2, "123456"))print ("使用新密碼登入鬼谷子叔叔/n")print (myTFSimpleTextDb.login("鬼谷子叔叔", "123456"))print (myTFSimpleTextDb.getLastLoginUserId())print ("刪除鬼谷子叔叔/n")print (myTFSimpleTextDb.delete(2))print ("再次登入鬼谷子叔叔/n")print (myTFSimpleTextDb.login("鬼谷子叔叔", "123456"))myTFSimpleTextDb.close()123456789101112131415161718192021222324252627

5. 超大檔案讀寫

超大檔案讀寫需要避免將檔案裡面的內容全部讀出來放到變數裡這種行為,因為變數都是存在於記憶體當中的,如果變數裡面的資料太多會把記憶體用光,就會導致系統癱瘓,這個太可怕了~~

5. 將一個10G檔案裡的“福哥”換成“鬼谷子叔叔”

這個時候我們可以通過開啟兩個檔案指標,一個檔案指標負責讀現有檔案,一個檔案指標負責寫新的臨時檔案,完成後刪除現有檔案,再將臨時檔案重新命名為原來的檔案,就搞定了~~

rootDir = os.path.dirname(os.path.abspath(__file__))   "//TFSE"newFile = rootDir   "//newFile.py"tmpFile = rootDir   "//newFile.py.tmp"foNew = open(newFile, "r")foTmp = open(tmpFile, "w")if foNew and foTmp:    while True:        tmpLine = foNew.readline()        if not tmpLine:            break        tmpLine = tmpLine.replace("福哥", "鬼谷子叔叔")        foTmp.write(tmpLine)    foNew.close()    foTmp.close()    os.unlink(newFile)    os.rename(tmpFile, newFile)fc = ""with open(newFile, "r") as fo:    fc  = fo.readline()print(fc)12345678910111213141516171819202122

6. 總結

好了,今天童鞋們跟著福哥系統地將Python語言操作資料夾、操作檔案的方法學習了一遍,有了這些技術之後,今後在專案當中處理各種有關資料夾/檔案的問題就不會發怵了!

要注意一點哦,資料夾操作、檔案操作屬於IO操作,是有一定風險的,一定不要把資料夾/檔案的路徑搞錯了,要不把系統檔案或者程式檔案寫壞了,系統有可能就完蛋了~~

https://m.tongfu.net/home/35/blog/513233.html