python學習day07

來源:互聯網
上載者:User

標籤:pop   pre   layer   time   blog   修改   時間   append   pen   

一:檔案操作

#codeing:UTF-8#__author__:Duke#date:2018/3/4/004#今天學習檔案操作# #讀檔案f = open(‘information‘,‘r‘,encoding=‘utf-8‘) #開啟一個檔案對象,用讀的方式data = f.read(5)# 並用read方法,無參數時讀出全部的內容,參數為字元數print(data)f.close()  #關閉檔案## #寫檔案f = open(‘information2‘,‘w‘,encoding=‘utf-8‘) #開啟一個檔案對象,用寫的方式f.write(‘hello world !‘)# 並用write方法f.write(‘duke‘)f.close()  #關閉檔案## #追加檔案內容f = open(‘information2‘,‘a‘,encoding=‘utf-8‘) #開啟一個檔案對象,用追加的方式f.write(‘hello ****!‘)# 並用write方法f.write(‘杜克‘)f.close()  #關閉檔案# r+  模式  在最後追加f = open(‘information‘,‘r+‘,encoding=‘utf-8‘) #開啟一個檔案對象,用讀的方式data = f.read(5)# 並用read方法,無參數時讀出全部的內容,參數為字元數print(data)f.write(‘\njoin‘)f.close()  #關閉檔案#先清空,可寫之後可讀f = open(‘information‘,‘w+‘,encoding=‘utf-8‘) #開啟一個檔案對象,用讀的方式f.write(‘join‘)print(f.tell())#寫完後游標在最後f.seek(0)   #調整游標,後才可以讀取print(f.readline())f.close()  #關閉檔案#複製檔案f_read = open(‘information2‘,‘r‘,encoding=‘UTF-8‘)f_write = open(‘information3‘,‘a‘,encoding=‘UTF-8‘)num = 0for line in f_read:    num = num+1    if num == 6:        line = ‘hello world\n‘    f_write.write(line)f_write.close()f_read.close()

二:檔案對象的方法

#codeing:UTF-8#__author__:Duke#date:2018/3/4/004#讀檔案f = open(‘information‘,‘r‘,encoding=‘utf-8‘) #開啟一個檔案對象,用讀的方式num = 0;for i in f:#迭代器方法讀取資料,也就是用一行取一行    num +=1    if num == 6:        i = ‘‘.join([i.strip(),‘iii‘])    print(i.strip())f.close()  #關閉檔案# #tell方法f = open(‘information‘,‘r‘,encoding=‘utf-8‘) #開啟一個檔案對象,用讀的方式print(f.tell())  #tell方法讀取游標位置print(f.read(1))print(f.tell())f.seek(0)  #調整游標位置print(f.tell())print(f.read(1))print(f.tell())f.close()  #關閉檔案import timef = open(‘information2‘,‘w‘,encoding=‘utf-8‘) #開啟一個檔案對象,用讀的方式f.write(‘duke‘)# 並用read方法,無參數時讀出全部的內容,參數為字元數f.flush()time.sleep(10)time.sleep(10)f.close()  #關閉檔案#flush 方法import time,sysfor i in range(30):    sys.stdout.write(‘*‘)    sys.stdout.flush()    time.sleep(0.1)#truncate 截斷方法f = open(‘information2‘,‘a‘,encoding=‘utf-8‘) #開啟一個檔案對象,用追加的方式f.truncate(2)f.close()  #關閉檔案

三:作業時間(三級菜單的增刪查改)

#codeing:UTF-8#__author__:Duke#date:2018/3/4/004file = open(‘information‘,‘r+‘,encoding=‘UTF-8‘)data = file.read()data = eval(data)file.close();# for key in data:#     print(key)current_layer = data;  #記錄當前所在級parent_layers =[ ];  #列表存放進入路徑flag = 0;while True:    for key in current_layer:        print(key);  #迴圈列印菜單    choice  =  input("[b:返回,d:刪除,a:增加 ,r:修改 ]>>>:").strip()  #輸入,並去除前後的空格形式的多餘字元    if len(choice) == 0:continue;    if choice in current_layer:        parent_layers.append(current_layer)   #進入下一層前先將他的前一層放到進入的路徑列表中        current_layer = current_layer[choice];   #將下一層賦值為當前層    elif choice == ‘b‘:        if parent_layers:            current_layer = parent_layers.pop ();  #取出它的父層,並賦值到當前層        elif flag ==1 :            break;        else:            flag +=1            print ("已經是最第一層,再按一次將退出程式")    elif choice == ‘d‘:        value = input("輸入你想刪除的值:>>")        current_layer.pop(value)    elif choice == ‘r‘:        value1 = input("輸入你想修改的值:>>")        current_layer.pop (value1)        value2 = input("輸入你想修改後的值:>>")        value2 = {value2: {}}        current_layer.update (value2)    elif choice == ‘a‘:        value = input("輸入你想增加的值:>>")        value = {value:{}}        current_layer.update(value)    else:    #判斷非法輸入        print("無該選項...");print(data)file2 = open(‘information‘,‘w‘,encoding=‘UTF-8‘)data = str(data)file2.write(data)file2.close()

 

python學習day07

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.