[英]Suggestions on ways to store file information using python
I've been looking around online for ways to store information in python and i came across a variety of ways to do this. 我一直在网上寻找在python中存储信息的方法,而我遇到了各种各样的方法来做到这一点。 I wanted to come to stackoverflow and see what you guys 'who are more experienced with python' would suggest in this scenario.
我想参加stackoverflow,看看在这种情况下“对python有更丰富经验的人”会建议些什么。
I'm doing a for-loop collecting all the files in a specific directory. 我正在做一个for循环,收集特定目录中的所有文件。 What I want to do is collect each file along with it's file size, filename, and filetype.
我要做的是收集每个文件以及它的文件大小,文件名和文件类型。 What would you guys recommend for this?
你们会为此推荐什么? The method used below i know is not good practice.
我知道以下使用的方法不是很好的做法。
import os
cacheDir = os.path.normpath('Q:\Qoros\Project_Assets\car\PublishedMXS')
mxsFiles = []
mxsNames = []
maxFilesize = []
for file in os.listdir(cacheDir):
if file.endswith(".mxs"):
filePath = cacheDir + '/' + file
prefix = file.split('_')
mxsFiles.append(filePath)
mxsNames.append(prefix[0])
# maxFilesize.append(filesize)
from collections import namedtuple
import glob
import os
cache_dir = os.path.normpath('Q:\Qoros\Project_Assets\car\PublishedMXS')
filespec = "*.mxs"
FileData = namedtuple("FileData", ["name", "prefix", "size"])
files = []
for fname in glob.glob(os.path.join(cache_dir, filespec)):
name = os.path.abspath(fname)
prefix = fname.split("_")[0]
size = os.stat(fname).st_size
files.append(FileData(name, prefix, size))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.