Skip to content

Instantly share code, notes, and snippets.

@NorimasaNabeta
Created October 6, 2021 12:39
Show Gist options
  • Save NorimasaNabeta/ba028976117bcbc28da974cd31e43df0 to your computer and use it in GitHub Desktop.
Save NorimasaNabeta/ba028976117bcbc28da974cd31e43df0 to your computer and use it in GitHub Desktop.
dir-list-then-put-it-excel script
# -*- mode: python; coding: utf-8-unix -*-
#
# Time-stamp: <2021-02-16 10:59:17 norim>
#
import os
import shutil
import re
from datetime import datetime
from optparse import OptionParser
from openpyxl import Workbook
import cv2
import numpy as np
#
#
#
def save_excel(dat, debug=False):
titles = ['file', 'ext', 'size', 'path']
if len(dat) > 0:
wb = Workbook()
ws = wb.create_sheet("sample")
ws_row0=1
ws_col0=1
for tl in titles:
ws.cell(row=ws_row0, column=ws_col0, value=tl)
ws_col0 = ws_col0 + 1
ws_row0 = ws_row0 + 1
for wk in dat:
ws_col0=1
for tl in titles:
ws.cell(row=ws_row0, column=ws_col0, value=wk[tl])
ws_col0 = ws_col0 + 1
ws_row0 = ws_row0 + 1
savefile = datetime.now().strftime("save_%Y%m%d%H%M%S.xlsx")
wb.save(savefile)
return
#
#
#
if __name__ == '__main__':
parser = OptionParser()
parser.add_option("-q", "--quiet",
action="store_false", dest="verbose", default=True,
help="don't print status messages to stdout")
(options, args) = parser.parse_args()
# outdir = datetime.now().strftime("framed%Y%m%d_%H%M%S")
# thisdir = os.getcwd()
filedat = []
for arg in args:
# Getting the current work directory (cwd)
# r=root, d=directories, f = files
for r, d, f in os.walk(arg):
for fname in f:
# file_ext = re.search(r"\.([^.]+)$", filename).group(1)
file_ext = os.path.splitext(os.path.basename(fname))[1][1:]
# print (fname, file_ext.lower())
if file_ext.lower() in ["zip", "mp4", "avi", "wmv", "mkv"]:
print (fname)
filedat.append({
'file': fname,
'path': r,
'ext': file_ext.lower(),
'size': os.path.getsize(os.path.join(r, fname)) })
save_excel(filedat, False)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment