Reputation: 783
My code is giving me a browse-folder window where I'm choosing a top folder dir, this is then stored in a variable. From there I'm looking for specifically named folders. Which are stored in a list. After finding these I want to find their sizes and paths.
So I want to store and easy access, multiple items in different lists. I tried to write the code without classes as I don't know how to make it work. I'm thankful for all suggestions and solutions to make the code better. I think I have to probably restructure the whole code.
Currently, it looks like this:
Folder = Folder1_Name, Folder2_Name, Folder3_Name
Path = Folder1_Path, Folder2_Path, Folder3_Path
Size = Folder1_Size, Folder2_Size, Folder3_Size
That is what I want:
Proj1 = Folder1_Name, Folder1_Size, Folder1_Path
Proj2 = Folder2_Name, Folder2_Size, Folder2_Path
Proj3 = Folder3_Name, Folder3_Size, Folder3_Path
My current Code
import os
import re
# Choose Folderpath
from tkinter import *
from tkinter import filedialog
root = Tk()
root.withdraw()
userpromp_path = filedialog.askdirectory()
os.chdir(userpromp_path)
#potencialprojects = ['1901_Default_Project2', '1910_Default_Project']
# Define String Match for Project
p = ''.join(["^", # Start of string being matched
"[0-2]", # First character a number 0 through 2 (less than 3)
"[6-9]", # Second character a number 6 through 9 (single digit greater than 5)
"(0(?=[0-9])|1(?=[0-2]))", # (lookahead) A 0 followed only by any number 0 through 9 **OR** A 1 followed only by any number 0 through 2
"((?<=0)[1-9]|(?<=1)[0-2])", # (lookbehind) Match 1-9 if the preceding character was a 0, match 0-2 if the preceding was a 1
"_", # Next char is a "_"
"[A-Z]", #Next char (only) is an upper A through Z
".*$" # Match anything until end of string
])
projectnames_found = []
projectpathes_found = []
projectsizes_found = []
def find_projects(searchpath):
for root, subdirs, files in os.walk(searchpath):
for projectname in subdirs:
if re.match(p, projectname):
projectnames_found.append(projectname)
projectpathes_found.append(os.path.join(os.getcwd(),projectname))
def get_size(scan_paths = '.'):
total_size = 0
for dirpath, dirnames, filenames in os.walk(scan_paths):
for f in filenames:
fp = os.path.join(dirpath, f)
if not os.path.islink(fp):
total_size += os.path.getsize(fp)
return round((((total_size/1000)/1000)/1000), 2), 'GB'
find_projects(userpromp_path)
for projectpath_found in projectpathes_found:
projectsizes_found.append(get_size(projectpath_found))
for a in projectnames_found, projectpathes_found, projectsizes_found:
print(a, '\n')
Upvotes: 0
Views: 47
Reputation: 607
What you need is the zip()
function (Link to documentation)
Your code should look something like this:
zipped = zip(Folder, Path, Size)
projects = list(zipped)
Now, projects
is a list where each index refers to a tuple containing each project's information. For example:
projects[0] # This contains the first project information
projects[0][0] # This is Folder1_Name
projects[0][1] # This is Folder1_Path
projects[1][2] # This is Folder2_Size
Upvotes: 1