-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathShelveWorkspaceScript.py
More file actions
56 lines (44 loc) · 1.6 KB
/
ShelveWorkspaceScript.py
File metadata and controls
56 lines (44 loc) · 1.6 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
#!/usr/bin/env python3
# -*- coding: utf-8 -*-
"""
Created on Mon Sep 16 08:09:15 2019
@author: thugwithyoyo
"""
import os
import shelve
# This script requires that the path variable SavePath be pre-defined
# in the scope from which this script is called.
#
# Example: code calling this script should be as follows:
#
# SavePath = '/home/thugwithyoyo/CaTransDecoding/Output/TestSave2'
# exec(open('./ShelveWorkspaceScript.py').read())
# Save path of directory that contains this script
ScriptDir = os.getcwd()
# Use path from function argument to change to save directory and write file
# with name contained in Filename.
(PathToFile, Filename) = os.path.split(SavePath)
# Change to directory that will contain file to be saved.
os.chdir(PathToFile)
# Open a shelf object to contain workspace variables.
my_shelf = shelve.open(Filename)
#PathToSaveFile = root.filename
#my_shelf = shelve.open(PathToSaveFile, 'n') # 'n' for new
# Iterate through list of "global" variables and write to file. Write each
# element to the my_shelf dictionary object.
#
# Remember that globals() actually generates a list of the variables restricted
# to within scope of the calling function.
for key in dir():
try:
#my_shelf[key] = globals()[key]
my_shelf[key] = locals()[key]
except TypeError:
#
# __builtins__, my_shelf, and imported modules can not be shelved.
#
print('ERROR shelving: {0}'.format(key))
# Close shelf object after variables have been written to file
my_shelf.close()
# Return to directory that contains this script.
os.chdir(ScriptDir)