简体   繁体   中英

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. I wanted to come to stackoverflow and see what you guys 'who are more experienced with python' would suggest in this scenario.

I'm doing a for-loop collecting all the files in a specific directory. 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))

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM