1. 程式人生 > 其它 >檔案簡單操作

檔案簡單操作

檔案讀寫模式

a	只追加模式(尾部追加)
	路徑不存在會自動建立新的檔案
	with open(r'b.txt','a',encoding='utf8') as f:
		f.write('hello\nMr')
		f.write('hello\nMr')
		f.write('hello\nMr')
	#.write()括號內增加資料就可以把資料新增到目標檔案內打印出來的都在同一行,如果要變行需要\n,寫入的內容會在尾部追加
	路徑存在會在檔案尾部追加

檔案

1.操作檔案

.read() # 一次性讀取檔案內所有內容

.readline() # 每次只讀檔案一行內容

.readlines

# 讀取檔案所有的內容,組織成列表,每個元素是檔案的每行內容

.readable() # 判斷當前檔案是否具備讀的能力

1.讀取檔案
	with open(r'b.txt','a',encoding='utf8') as f:

.wrint() #()內容直接寫入到文字內容,寫入的內容必須是字串型別

.weitlines() # 可以將列表中多個字串元素全部寫入

.wrtable() # 判斷檔案是否擁有可讀的能力

.flush # 直接將記憶體中的檔案資料刷到硬碟中,相當於ctrl+s\

2.檔案優化操作

	with open(r'b.txt','a',encoding='utf8') as f:
	1.一次性讀完之後 游標停留在了檔案末尾 無法再次讀取內容
	2.該方法在讀取大檔案的時候 可能會造成記憶體溢位的情況
		解決上述問題的策略就是逐行讀取檔案內容

	# for i in f:  # 檔案變數名f支援for迴圈  相當於一行行讀取檔案內容
	# 以後涉及到多行檔案內容的情況一般都是採用for迴圈讀取'''

3.檔案操作模式

t	文字模式
	1.預設模式
		r	w	a	>>>		rt	wt	at
	2.該模式所有操作都是以字串為基本單位(文字)
	3.該模式必須要有指定的encoding引數

b	二進位制模式
	1.該模式可以操作任意型別的檔案
	2.該模式所有的操作都是以bytes型別(二進位制)基本單位
	3.該模式不需要指定encoding引數
    	rb	wb	ab
	with open(r'b.txt','rb') as f:
        print(f.read)
	b'\xe4\xbd\xa0\xe5\xa5\xbd\r\n\xe4\xbd\xa0\xe5\xa5\xbd\r\n1\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x91\xa2\r\nhello2hello\r\nMr3hello\r\nMr4hello\r\nMr1\xe4\xbd\xa0\xe5\xa5\xbd\xe5\x91\xa2\r\nhello2hello\r\nMr3hello\r\nMr4hello\r\nMr1\xe5\x91\xa2\r\nhello2hello\r\nMr3hello\r\nMr4hello\r\nMr1\r\nhello2hello\r\nMr3hello\r\nMr4hello\r\nMr1hello2helloMr3helloMr4helloMr'
#顯示的為b>>>bytes型別,中文三個位元組為一個基本單位
#讀取來的內容
	with open(r'b.txt','ab') as f:
        f.write('')
		#這樣括號內寫不是二進位制型別會報錯
#b模式要寫內容到檔案需要二進位制型別。
	with open(r'b.txt','rb') as f:
		f.write(''.encode('utf8'))
#將要寫入的內容更改為二進位制型別,儲存到檔案後會轉化為寫入的內容
        

練習

拷貝

# 首先選擇一個檔案,作為拷貝的目標
with open(r'01.py','rb') as l1:
    # 將這個檔案裡面的資料讀出來之後賦值
    l2 = l1.read()
# 建立一個新的檔案
with open(r'複製.txt','wb') as l3:
    # 將剛取出來的檔案寫入
    l3.write(l2)

使用者簡單註冊

while True:
    print("""
    請選擇功能;
    1.使用者註冊
    2.使用者登入
    
    """)
    suerinput = input('請輸入:').strip()
    if suerinput == '1':
    # 1.獲取使用者輸入
        name = input('請輸入使用者名稱:').strip()
        password = input('請輸入密碼:').strip()
        # 1.1將使用者輸入的內容兩條資料做成一條
        information = '{}|{}\n'.format(name, password)
        # 2.判斷使用者輸入的使用者名稱是否重複、需要將檔案裡面的資料取出來
        with open(r'作業練習.txt','r',encoding='utf8') as l1:
            # 2.1迴圈取出裡面每一條資料
            for i in l1:
                # 2.2因為取出來的是一個字串,要將裡面的資料變成列表,在比對使用者名稱
                name1,password2 = i.split('|')
                # 2.3做成列表之後比對使用者輸入的使用者名稱存不存在檔案中
                if name == name1:
                    # 2.4如果重複就告知使用者
                    print('使用者名稱重複,請重新輸入!')
                    # 2.5告知過後結束這個for迴圈
                    break
            # 當for迴圈不是自然結束後面的else不會執行
            else:
                # 1.2建立一個檔案儲存資料
                with open(r'作業練習.txt','a',encoding='utf8') as practice:
                    #1.3 將得到一條字串通過AB模式寫入到新建的資料夾只能夠
                    practice.write(information)
                # 1.4告知使用者建立成功
                print('使用者{}建立成功'.format(name))
    elif suerinput == '2':
        # 需要結束此迴圈
        count = True
        while count:
#           3.獲取使用者輸入
            My_name = input('請輸入登入姓名;').strip()
            My_password = input('請輸入密碼:').strip()
            #3.1 取出資料夾內的資料
            with open(r'作業練習.txt','r', encoding='utf8') as l2:
                # 3.2將資料夾裡面的資料進行迴圈取出
                for l3 in l2 :
                    # 3.3從字串變成列表然後取出名字密碼對比
                    uer_name,uer_passwold = l3.split('|')
                    #3.4 如果賬號密碼都正確就會告知登入成功,並結束
                    if uer_name == My_name and uer_passwold.strip('\n') == My_password:
                        print('登入成功')
                        # 3.5如果程式走到這裡就會把while的迴圈變成False,結束迴圈
                        count = False

                    else:
                        #密碼錯誤則會告知使用者,並重新迴圈
                        print('賬號或者密碼錯誤,請重新輸入 ')