日韩性视频-久久久蜜桃-www中文字幕-在线中文字幕av-亚洲欧美一区二区三区四区-撸久久-香蕉视频一区-久久无码精品丰满人妻-国产高潮av-激情福利社-日韩av网址大全-国产精品久久999-日本五十路在线-性欧美在线-久久99精品波多结衣一区-男女午夜免费视频-黑人极品ⅴideos精品欧美棵-人人妻人人澡人人爽精品欧美一区-日韩一区在线看-欧美a级在线免费观看

歡迎訪問 生活随笔!

生活随笔

當前位置: 首頁 > 编程语言 > python >内容正文

python

python文件下载速度 装饰器_python使用装饰器对文件进行读写操作'及遍历文件目录...

發布時間:2025/3/12 python 17 豆豆
生活随笔 收集整理的這篇文章主要介紹了 python文件下载速度 装饰器_python使用装饰器对文件进行读写操作'及遍历文件目录... 小編覺得挺不錯的,現在分享給大家,幫大家做個參考.

‘‘‘使用裝飾器對文件進行讀寫操作‘‘‘

#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使用

importpickle

srcurl=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 inlines:

pickle.dump(line,dest)

with open(r‘c:\Users\PGIDYSQ\Desktop\tset.bat‘,‘rb‘) as fp:

n= pickle.load(fp)#轉換行數號

for i inrange(n):#print(pickle.load(fp))

bb =pickle.load(fp)print(bb)#struct使用#struct.pack,unpack==》write(struct),read(9)

‘‘‘遍歷指定目錄下的所有子目錄及文件‘‘‘

importosdefvisitDir(path):if notos.path.isdir(path):print(‘Error‘)return

for lists inos.listdir(path):

sub_path=os.path.join(path,lists)print(sub_path)ifos.path.isdir(sub_path):

visitDir(sub_path)#遞歸調用#visitDir(r‘F:\UpSVNProject‘)#采用os.walk()方法進行遍歷

defvisitDir2(path):if notos.path.isdir(path):print(‘Error‘)returnlist_dirs=os.walk(path)for root,dirs,files inlist_dirs:for d indirs:print(os.path.join(root,d))for f infiles:print(os.path.join(root,f))#visitDir2(r‘F:\UpSVNProject‘)

總結

以上是生活随笔為你收集整理的python文件下载速度 装饰器_python使用装饰器对文件进行读写操作'及遍历文件目录...的全部內容,希望文章能夠幫你解決所遇到的問題。

如果覺得生活随笔網站內容還不錯,歡迎將生活随笔推薦給好友。