# coding=gbk import os import os.path #读取目录下的所有文件,包括嵌套的文件夹 def GetFileList(dir, fileList): newDir = dir if os.path.isfile(dir): fileList.append(dir) elif os.path.isdir(dir): for s in os.listdir(dir): # 如果需要忽略某些文件夹,使用以下代码 # if s == "xxx": # con
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 # 统计