/
ColabDriveFiles.py
90 lines (82 loc) · 2.91 KB
/
ColabDriveFiles.py
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
# 12/05/2020
from __future__ import absolute_import
import os, shutil, sys
from os.path import *
from glob import glob
from pathlib import Path
from BashColors import C
class ColabDriveFiles:
'''List all files in Collab '''
def __init__(self):
self.cdf=None
self.contentPth='/content'
self.drivePth='/content/drive'
self.myDrivePth='/content/drive/My Drive'
self.pythonFilesPth='/content/drive/My Drive/PythonFiles'
self.allDirectoriesDict={}
self.allFilesDict={}
self.allFiles=self.updateFileList().copy()
def clearAllCaches(self, silent=True):
'''clear all __pycache__ and content'''
all_files = self.allFiles
for fil in all_files:
if isdir(fil) and fil.__contains__('__pycache__'):
cache_pth=realpath(fil)
if not silent:
print(f'{C.IRed}{cache_pth}')
shutil.rmtree(fil)
else: pass
def cleanDrive(self):
'''removes sample_data directory and contents'''
os.chdir('/content')
delete_path='/content/sample_data'
if exists(delete_path):
shutil.rmtree(delete_path)
self.updateFileList()
else: pass
def updateFileList(self):
'''Updates self.allFiles'''
os.chdir('/content')
file_list=glob('**', recursive=True)
return file_list
def listColabFiles(self, silent=False):
'''List all files available in Collab
populate self.allDirectoriesDict
populate self.allFilesDict'''
dirCount=1
dirDict={}
fileCount=0
fileDict={}
self.updateFileList()
if not '/content' in sys.path.copy():
sys.path.append('/content')
if dirCount==1 and dirDict=={}:
dirDict[1]='/content'
else: pass
if not silent:
print(f'{C.Blue}{Path.cwd()}')
for fil in sorted(self.allFiles):
fullPath=abspath(fil)
if isdir(fullPath) and not fullPath.__contains__('__pycache__'):
if not fullPath in sys.path.copy():
sys.path.append(fullPath)
dirCount+=1
dirDict[dirCount]=fullPath
if not silent:
print(f'\n{C.Blue}{fullPath}')
elif isfile(fullPath) and not fullPath.__contains__('__pycache__'):
fileCount+=1
fileDict[fileCount]=fullPath
if not silent:
print(f'{C.White}{fileCount}. {basename(fullPath)}')
self.allDirectoriesDict=dirDict.copy()
self.allFilesDict=fileDict.copy()
def printSystemPaths(self):
'''list system paths'''
pathCount=0
sysPaths=sys.path.copy()
sysPaths=sorted(sysPaths)
for pth in sysPaths:
pathCount+=1
print(f'{pathCount}. {C.Blue}{pth}')
cdf=ColabDriveFiles()