import pickle
import os.path


class FileDB():

def __init__(self, filePath, exten=""):
self.filePath = filePath.strip()
self.dbName = 'fileDB.dat'
self.dbText = ""
self.cats = {'Name':5, 'Date':5, 'Comment':8, 'Path':5}
self.fileDB = []
try:
fH = open(self.dbName, "rb")
self.db = pickle.load(fH)
self.fileDB = [data for data in self.db if data['Path'] == filePath]
fH.close()
except:
self.db = []
self.fileDB = []
self.updateDB()
self.updateDBText(self.fileDB)
self.writeDB()


#해당 주소에 파일을 DB에서 가져오거나 추가
def updateDB(self):
filePath = self.filePath
files = os.listdir(filePath)
fileList = [file for file in files if file.endswith("")]
#DB에서 사라진 파일 제거
for data in self.fileDB[:]:
if data['Name'] not in fileList:
self.fileDB.remove(data)

#DB없는 파일 추가
for file in self.fileDB:
for cat in file.keys():
if cat not in self.cats.keys():
self.cats[cat] = len(cat) + 1
for file in fileList:
if not(file in [data['Name'] for data in self.fileDB]):
dict = {}
for cat in self.cats:
dict[cat] = ''
dict['Name'] = file
dict['Path'] = filePath
self.fileDB.append(dict)
#시간 업데이트
for file in self.fileDB:
file['Date'] = str(os.path.getmtime(filePath + '/' + file['Name']))

def updateDBText(self, fileDB, key='Name'):
dbText = ""
db = fileDB
# 공백 간격 설정
for file in db:
for cat in self.cats:
self.cats[cat] = max(len(file[cat]), self.cats[cat])
# 카테고리 표시
for cat in self.cats:
dbText += cat + (" " * (self.cats[cat] + 4 - len(cat)))
dbText += '\n'
# 파일 표시
sortedDB = sorted(db, key=lambda x: x[key])
for file in sortedDB:
for cat in self.cats:
dbText += file[cat] + (' ' * (self.cats[cat] + 4 - len(file[cat])))
dbText += '\n'
#누락된 파일 표시
for file in self.fileDB:
if file not in db:
for cat in self.cats:
dbText += "\033[90m" + file[cat] + (' ' * (self.cats[cat] + 4 - len(file[cat])))
self.dbText = dbText

def writeDB(self):
for data in self.db[:]:
if data['Path'] == self.filePath:
self.db.remove(data)
self.db += self.fileDB
fH = open(self.dbName, "wb")
pickle.dump(self.db, fH)
fH.close()

#특정 카테고리의 파일 검색
def searchCat(self, cat):
searchedFiles = []
for file in self.fileDB:
if file[cat] != "":
searchedFiles.append(file)
self.updateDBText(searchedFiles, key=cat)

#특정 카테고리 삭제
def delCat(self, cat):
self.cats.pop(cat)
for file in self.fileDB:
file.pop(cat)
self.updateDBText(self.fileDB)

#특정 카테고리 추가
def addCat(self, cat):
self.cats[cat] = len(cat) + 1
for file in self.fileDB:
file[cat] = ""
self.updateDBText(self.fileDB)

#특정 파일 정보 수정
def setInfo(self, fileName, cat, info):
for file in self.fileDB:
if file['Name'] == fileName:
file[cat] = info.strip()
self.updateDBText(self.fileDB)

def getDBText(self):
return self.dbText

if __name__ == '__main__':
a = 'C:/Users/User/Desktop/사진/3. 보정후/'
## b = '/home/bumik/swp2/14'
file_db = FileDB(a)
print(file_db.dbText)




file_db.writeDB()


형님들 코딩뉴비 질문좀 받아주십숑 ㅠㅠㅠ

특정 폴더 주소를 변수 a에 담고 이제 실행시키면 그 주소의 파일들을 피클로 읽어서 데이터파일로 저장하는 코드를 짰는데요

이게 출력이 딴건 다 잘 되는데 date 값, 그니깐 날짜가 아래모양으로 나와요.. 왜 그런건가요??

저걸 우리가 아는 일반적인 날짜?로 바꾸는 방법은 없나요???