you have many many folders where each folder has a name consisting of 8 lowercas
ID: 3593241 • Letter: Y
Question
you have many many folders where each folder has a name consisting of 8 lowercase letters or digests.
Write a program that analyzes the folder structure.
1) Find the path to the folders that have capital letters in their name.
2) Find the path to the 6 files that are inside the folders.
do NOT use os.walk.
#this function returns a set of file names
#including their paths, e.g., #start/38ask9sd/adsf928/saf828a/blub.lnk
def get_files():
return {}
#this function only returns the file names (without the path)
#it should use get_files() to create the return value
def get_file_names():
A = get_files()
return {}
#this function returns a set containing
#the full path to a capitalized folder
#start/38ask9sd/ADSF928/
def get_cap_folders():
return {}
# parses the folder structure and prints
# the paths to the files and upper case
# folders respectively
def parse_folder_structure(start='start'):
print("all upper case folder paths'")
print("all file paths'")
code needed in python V3.x
Explanation / Answer
Hi, i have solved your problem.
I created 4 sub-folders in a directory, with 1 foldername in CAPS LOCK.
packages used- glob #alternative for os.walk to listing files & folders.
os.basename for gettiing to root file in a folder.
pathlib, for getting foldernames in capslock.
Below is my code. ANy questions you can contact me:
'folders_que' is a directory which contatins 4 subfolders Untitled Folder 1,Untitled Folder 2,Untitled Folder 3, UNTITLED4.
Each subfolder having different files.
You can change the path accordingly while running.
from glob import glob
from os.path import basename
from pathlib import PureWindowsPath
def get_files(path):
path = path+'/*/'
L = glob(path)
Final_file_list = []
for i in L:
temp = glob(i+'*')
Final_file_list.extend(temp)
return Final_file_list
get_files("/home/admin/Desktop/folders_que")
def get_file_names(path):
path = path+'/*/'
A = get_files(path)
fileNames=[]
for i in A:
temp=basename(i)
fileNames.append(temp)
return fileNames
get_file_names("/home/admin/Desktop/folders_que")
def get_cap_folders(path):
main_path = path
main_path2 = path+'/*/'
path = glob(main_path2)
folder_list = [PureWindowsPath(p).name for p in path]
caps_folder=[word for word in folder_list if word.isupper()]
final_list = []
for i in caps_folder:
final_list.append(main_path+'/'+i)
return final_list
get_cap_folders("/home/admin/Desktop/folders_que")
def parse_folder_structure(path):
cap_folder_list = get_cap_folders(path)
print("all upper case folder paths: ")
print(cap_folder_list)
all_file_list=[]
for i in cap_folder_list:
filepath = glob(i+'/*')
all_file_list.extend(filepath)
print("all file paths': ")
print(all_file_list)
parse_folder_structure("/home/admin/Desktop/folders_que")
Related Questions
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.