-
Notifications
You must be signed in to change notification settings - Fork 2
Expand file tree
/
Copy pathtest_sqlAchemy.py
More file actions
62 lines (38 loc) · 1.29 KB
/
Copy pathtest_sqlAchemy.py
File metadata and controls
62 lines (38 loc) · 1.29 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#!/usr/bin/env python
from sqlalchemy import create_engine
from sqlalchemy import Table,Column,Integer,String,MetaData,ForeignKey
from sqlalchemy.orm import mapper,sessionmaker
import os
path = '/tmp'
#part 1:create engine
engine = create_engine('sqlite:///:memory:',echo=False)
#Part 2: metadata
metadata = MetaData()
filesystem_table = Table('filesystem',metadata,
Column('id',Integer,primary_key=True) ,
Column('path',String(500)),
Column('file',String(255)),
)
metadata.create_all(engine)
#Part 3:mapped class
class FileSystem(object):
def __init__(self,path,file):
self.path = path
self.file = file
def __repr__(self):
return "[FileSystem('%s','%s')]" % (self.path,self.file)
#Part 4:mapper function
mapper(FileSystem,filesystem_table)
#Part 5:create sesstion
Session = sessionmaker(bind=engine,autoflush=True)
sesstion = Session()
#Part 6:crawl file system and populate databse with results
for dirpath,dirnames,filenames in os.walk(path):
for file in filenames:
fullpath = os.path.join(dirpath,file)
record = FileSystem(fullpath,file)
#Part 7:commit to the database
sesstion.commit()
#Part 8:query
for record in sesstion.query(FileSystem):
print "database record Number:%s,File:%s " % (record.id,record.path,record.file)