1. 程式人生 > 其它 >ptyhon之檔案處理

ptyhon之檔案處理

檔案

1、什麼是檔案
    檔案是作業系統提供給使用者或者說應用程式操作硬碟的一種功能

2、為何要用檔案
    讀寫檔案就是在讀寫硬碟:我們對檔案的讀寫操作都會被作業系統轉換成硬碟的讀寫操作

    應用程式-------------》檔案物件、檔案控制代碼
    作業系統-------------》檔案
    硬體-----------------》硬碟

3、如何用檔案

      # f = open(r'a.txt', mode='rt', encoding='utf-8')
      # print(f)  # 檔案物件、檔案控制代碼

        #res = f.read()
        #print(res)

          #f.close()   關閉檔案

          # print(f)

          # f.read()   讀取檔案

          # del f      刪除檔案
with語句
with open('a.txt', mode='rt', encoding='utf-8') as f,\
    (開啟)(文字檔案)(讀寫操作和內容模式)(編碼方式)
        open('b.txt',mode='wt',encoding='utf-8') as f1:
    pass

檔案開啟模式

  1、控制檔案讀寫操作的模式

          r(預設)讀

          w       寫

          a       追加寫

    2、控制檔案讀寫內容的模式

        t(預設的):讀寫都是以字串為單位的,只適用於檔案檔案,必須指定encoding引數

        b:讀寫都是以bytes為單位的,適用於所有檔案,一定不能指定encoding引數


    r模式:只讀模式,如果檔案不存在則報錯,檔案存在則檔案指標處於檔案開頭

        # f = open('a.txt',mode='rt',encoding='utf-8')

        # print('='*50)   #列印‘=’50行

        # print(f.read()) #讀檔案f

        # print(f.readable())  #判斷是否只讀  輸出True

        # print(f.writable())  #判斷是否只寫  輸出Flase

        # f.close()  #關閉檔案

    w模式:只寫模式,如果檔案不存在則建立空文件,檔案存在則清空,檔案指標處於檔案開頭

        # f = open('a.txt',mode='wt',encoding='utf-8')

        # f.write("你好\n")

        # f.write("哈哈哈\n")

        # f.write("我擦勒\n")



        # f.write("你好\n哈哈哈\n我擦勒\n")

        # print(f.readable())

        # print(f.writable())

        # f.close()

    總結:w模式,在打開了檔案不關的情況下,連續的write寫入,新寫的內容永遠跟在後面

        a模式:只追加寫模式,如果檔案不存在則建立空文件,檔案存在不會清空,檔案指標處於檔案末尾

        # f = open('c.txt',mode='at',encoding='utf-8')

        # f.write("4444\n")

        # f.write("555\n")

        # f.write("6666\n")

        # print(f.readable())

        # print(f.writable())

        # f.close()

    總結:a模式,在打開了檔案不關的情況下,連續的write寫入,新寫的內容永遠跟在後面,這一點與w模式相同

        不同的是,在打開了檔案關閉然後重新開啟的情況下,a模式永遠寫在後面


        b模式:讀寫都是以bytes為單位的,適用於所有檔案,一定不能指定encoding引數

        # with open('1.mp4',mode='rb') as f:

        #     res = f.read()

        #     print(res.decode('utf-8'))


        # with open('a.txt',mode='rb') as f:

        #     res = f.read()

        #     print(res.decode('utf-8'))


        # with open('a.txt',mode='ab') as f:

          #     f.write("啊手動閥手動閥".encode('utf-8'))


        例:檔案拷貝功能

          方案一:

            # with open('1.mp4',mode='rb') as f1,open(r'D:\1111111.mp4',mode='wb') as f2:

            #     res = f1.read()

            #     f2.write(res)


        方案二:

            儲備知識:

            # with open('a.txt',mode='rt',encoding='utf-8') as f:

            # f.read()

            #     for line in f:

            #         print(line,end='')


            # with open('1.mp4',mode='rb') as f:

            #     for line in f:

            #         print(line)

        具體實現:

             # with open('1.mp4',mode='rb') as f1,open(r'D:\1111111.mp4',mode='wb') as f2:

             #     for line in f1:

             #         f2.write(line)


        # 可讀可寫

        # r+t

        # w+t

        # a+t

        # r+b

        # w+b

        # a+b

            # with open('a.txt',mode='r+t',encoding='utf-8') as f:

            #     print(f.readable())

            #     print(f.writable())

            #     print(f.read())

            #     f.write("22222222222222222222\n")



            # with open('a.txt',mode='w+t',encoding='utf-8') as f:

            #     print(f.readable())

            #     print(f.writable())

檔案操作其他方法


        # with open('a.txt',mode='rt',encoding='utf-8') as f:

                # lines = f.readlines()   #一下子全讀出來

                # print(lines)

                # print(f.readline())   #只讀一行


        # with open('a.txt',mode='wt',encoding='utf-8') as f:

                # f.write("111\n222\n333\n")

                # lines = ['111\n','222\n','3333\n']

                # for line in lines:  #迴圈將lines裡的給line

                #f.write(line)      


                # f.writelines(lines)   #一下子寫入


                #     f.write('hello')

                #     f.flush()   #重新整理
                # f.writelines('hello')

檔案指標的移動

    大前提:只有t模式下的,f.read(n)中的n代表的是字元個數,除此以外,關於指標的移動,單位都是位元組bytes


        # with open('a.txt',mode='rt',encoding='utf-8') as f:

        #     res = f.read(6)

        #     print(res)

        # with open('a.txt',mode='rb') as f:

        #     # res = f.read(6)

        #     res = f.read(8)

        #     print(res.decode('utf-8'))

        # f.truncate()   #擷取

        # with open('a.txt',mode='r+t',encoding='utf-8') as f:

        #     f.truncate(8)



        # f.seek移動的全都是位元組個數

        #f.seek(位元組個數,0)
        #f.seek(位元組個數,1)
        #f.seek(位元組個數,2)
            ps:只有0模式可以t下使用,其中1和2模式只能在b模式下使用,但是無論在t模式還是b模式下,移動的都是位元組個數

        # with open('a.txt',mode='rt',encoding='utf-8') as f:

                # print(f.tell())    #告訴我們指標移動了多少

                # f.seek(9,0)

                # f.seek(3,0)



        # with open('a.txt',mode='rb') as f:

        #     print(f.tell())

        #     f.seek(9,1)

        #     f.seek(3,1)

        #     f.seek(0,2)

        # with open('a.txt',mode='ab') as f:

        #     print(f.tell())
#檔案修改的原理:

	把硬碟資料讀入記憶體,在記憶體修改完畢後,再覆蓋回硬碟

		具體來說又分為兩種方案

		方案一:

			# with open('test.txt',mode='rt',encoding='utf-8') as f:

			#     data = f.read()

			# with open('test.txt',mode='wt',encoding='utf-8') as f:

		#     f.write(data.replace('egon','EGON'))


		方案二:

		import os   #os模組

		with open('test.txt',mode='rt',encoding='utf-8') as f1,\
        	open('.test.txt.swp',mode='wt',encoding='utf-8') as f2:
    		for line in f1:
        		f2.write(line.replace('EGON',"egon"))

		os.remove('test.txt')    #刪除
		os.rename('.test.txt.swp','test.txt')  #改名字