Python3.3下测试通过 获取文件大小 使用os.path.getsize函数,参数是文件的路径 获取文件夹大小 import os from os.path import join, getsize def getdirsize(dir): size = 0.0 for root, dirs, files in os.walk(dir): size += sum([getsize(join(root, name)) for name in files]) return size if __…
1 #获取文件夹内的图片 2 import os 3 def get_imlist(path): 4 return [os.path.join(path,f) for f in os.listdir(path) if f.endswith('.jpg')] 5 6 img_path = get_imlist("database") 7 print(img_path)…
1, 使用文件 #vim /etc/motd "1 hello world" 2 ...... yes 3 no you are a shadiao 4 hahh maye you are right ddddddddddddddddddddddddddddddddddd ccccccccccccc vvv 2,python脚本 [root@localhost python]# vim 7.py f=open("/etc/motd") alllines = [lin…
import os totalSize = 0 fileNum = 0 dirNum = 0 def visitDir(path): global totalSize global fileNum global dirNum for lists in os.listdir(path): sub_path = os.path.join(path, lists) print(sub_path) if os.path.isfile(sub_path): fileNum = fileNum+1 # 统计…
__author__ = 'bruce' import os from os.path import join,getsize def getdirsize(dir): size=0l for (root,dirs,files) in os.walk(dir): for name in files: try: #print getsize(join(root,name)) size += getsize(join(root,name)) except: continue #直接用下面这句代码,在…
import os path = r'/Users/authurchen/PycharmProjects/Demo' # print(os.listdir(path)) ls = os.listdir(path)files = [] def get_all_file(dir_path): global files for filepath in os.listdir(dir_path): tmp_path = os.path.join(dir_path,filepath) if os.path.…
第一种:使用os.walk: # -*- coding: utf-8 -*- import os def Test1(rootDir): list_dirs = os.walk(rootDir) 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) 第二种:使用os.listdir: # -*- codin…
def GetFileMd5(filename): if not os.path.isfile(filename): return myhash = hashlib.md5() f = file(filename,'rb') while True: b = f.read(8096) if not b : break myhash.update(b) f.close() return myhash.hexdigest()…