python使用裝飾器對檔案進行讀寫操作'及遍曆檔案目錄

來源:互聯網
上載者:User

標籤:rmi   函數定義   root   調用   AC   coding   增加   IV   enum   

‘‘‘使用裝飾器對檔案進行讀寫操作‘‘‘# def check_permission(func):#     ‘‘‘示範嵌套函數定義及使用‘‘‘#     def wrapper(*args,**kwargs):#         ‘‘‘*args:接收任意多個實參並存入元組中;**kwargs:接收關鍵字參數顯示賦值並存入字典中‘‘‘#         if kwargs.get(‘username‘) != ‘admin‘:#             raise Exception(‘Sorry,You are not allowed‘)#         return func(*args,**kwargs)#     return wrapper## class ReadWriteFile(object):#     # 裝飾器#     @check_permission #read = check_permission(read)#     def read(self,username,filename):#         return open(filename,‘r‘).read()##     def write(self,username,filename,content):#         with open(filename,‘a+‘) as op:#採用with上下文管理語句#             op.write(content)#         #open(filename,‘a+‘).write(content)#     # 普通函數使用#     writes = check_permission(write)## t = ReadWriteFile()# print(t.read(username=‘admin‘,filename=r‘c:\Users\PGIDYSQ\Desktop\1111111e.gen‘))# print("*"*60)# t.write(‘admin‘,filename=r‘c:\Users\PGIDYSQ\Desktop\1111111e.gen‘,content=‘增加內容...‘)# print("-"*60)# with open(r‘c:\Users\PGIDYSQ\Desktop\1111111e.gen‘) as fp:#     #print(list(map(len,fp.readlines())))#     print(list(enumerate(fp.readlines())))#         # for line in fp:#         # print(line)#pickle使用import picklesrcurl =r‘c:\Users\PGIDYSQ\Desktop\1111111e.gen‘dsturl =r‘c:\Users\PGIDYSQ\Desktop\tset.bat‘with open(srcurl,encoding=‘utf-8‘) as src,open(dsturl,‘wb‘) as dest:    lines = src.readlines()    pickle.dump(len(lines),dest)#行數    for line in lines:        pickle.dump(line,dest)with open(r‘c:\Users\PGIDYSQ\Desktop\tset.bat‘,‘rb‘) as fp:    n = pickle.load(fp)#轉換行數號    for i in range(n):        #print(pickle.load(fp))        bb = pickle.load(fp)        print(bb)#struct使用#struct.pack,unpack==》write(struct),read(9)‘‘‘遍曆指定目錄下的所有子目錄及檔案‘‘‘import osdef visitDir(path):    if not os.path.isdir(path):        print(‘Error‘)        return    for lists in os.listdir(path):        sub_path = os.path.join(path,lists)        print(sub_path)        if os.path.isdir(sub_path):            visitDir(sub_path)#遞迴調用#visitDir(r‘F:\UpSVNProject‘)#採用os.walk()方法進行遍曆def visitDir2(path):    if not os.path.isdir(path):        print(‘Error‘)        return    list_dirs = os.walk(path)    for root,dirs,files in list_dirs:        for d in dirs:            print(os.path.join(root,d))        for f in files:            print(os.path.join(root,f))#visitDir2(r‘F:\UpSVNProject‘)

 

python使用裝飾器對檔案進行讀寫操作'及遍曆檔案目錄

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在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.