這也花超久的時間去了解,總之解決了。
原因是這樣的:
假設我現在有個資料夾,裡面有放數支手機資料夾,每隻手機資料夾裡面都還有好幾個不等的數據資料夾,在裡面又都各有個txt文檔。
當數量眾多時,有時在讀檔不確定是當機了還是還在處理數據,這時就會希望能夠有個進度條來顯示目前進度.
網路上很多基本的進度條code供參考,其實那也可以,但是看到jupyter notebook 有個互動式的進度條,覺得很酷,想拿來嘗試。
找到網路上有個人分享他寫的方式:
結果如以下的圖示:
由於他是寫成function,所以要套用我的狀況,就勢必要做修改。本來嘗試幾天有點想放棄,覺得其實也沒必要這樣麻煩,但又覺得不想放棄,所以終於讓我想到怎修改了(其實好像是自己腦筋差)
以下是我的改法,由於完整碼滿多的,所以只付上progress的部分,待我把我目標的code完成,會再一併放到github上分享:
# 進度條寫法:
import sys
from time import sleep
import re
def progress(No_folder,every =1, size=None):
    from ipywidgets import IntProgress, HTML, VBox
    from IPython.display import display
    progress = IntProgress(min=0, max=len(M_filefolder))
    label = HTML()
    box = VBox(children=[label, progress])
    display(box)
    try:
        for index, record in enumerate(M_filefolder, 1):
            #print ( index  )   
            if index % every == 0:
                path_with_M_Lipid = path_with_phoneNumber +'/'+ M_filefolder[index-1]    
                temp = os.getcwd()    
                os.chdir( path_with_M_Lipid )
                path_forProgress = os.getcwd()
                for root, dirs, files in os.walk(path_forProgress):
                    dirs
                    files
                    break
                method = r'.*\.txt$'  # 用來確認檔案裡有txt
                count = 0
                countError = 0
                list_item =[]
                for item in os.listdir(path_forProgress): # 刪除刪除隱藏檔
                    if not item.startswith('.') :
                        list_item.append(item)
                for ii in range(len(list_item)): # Use len() to get how many files in folder
                    filename = list_item[ii]
                    #print filename
                    answer = re.search(method,filename)
                    if bool(answer):
                        print answer.group ()
                        count = count +1
                        txtfile=(filename)
                if count == 0 :
                    countError = countError+1
                if countError != 0:
                    print 'Folder:[ '+'{}'.format(M_filefolder[index-1])+' ]'+' without txt file'   
                os.chdir(path_with_phoneNumber)
            os.chdir(path_)
            sleep(0.1) 
            progress.value = index
            label.value = '{index} / {size}'.format(
                index=index,
                size=len(M_filefolder)
            ) 
            yield record  #回傳給url index為順序編號 record為物件內容
    except:
        progress.bar_style = 'danger'
        raise
    else:
        progress.bar_style = 'success'
        progress.value = index
        label.value = str(index )

| 這封郵件來自 Evernote。Evernote 是您專屬的工作空間,免費下載 Evernote | 

0 意見:
張貼留言