Reputation: 27
I am looking for support on creating a folder structure in Python. I have basic Python knowledge but trying to see how it can automate some mundane tasks.
I have a xlxs with a list of 1000 names in the same directory as my Python script called names.xlxs. These are the headings it has.
I would like to create a folder,with a concatenation of the name and tutor group, for each person on the list, within that folder I would also like the following sub-folders.
AoW
CSP
Folder A
Folder B
Folder C
Folder D
Folder E
Folder F
Can someone point me in the right direction of the best way to do this?
Upvotes: 0
Views: 2253
Reputation: 23099
Let's try pathlib
which makes working with the file system a breeze. Note, you'll need Python 3.5+ to use it.
import pandas as pd
from pathlib import Path
df = pd.DataFrame({'Name' : 'Joe Bloggs', 'Tutor Group' : '6T1'},index=[0])
sub_folders = ['AoW',
'CSP',
'Folder A',
'Folder B',
'Folder C',
'Folder D',
'Folder E',
'Folder F']
Assuming you know how to read in an Excel file into a dataframe and it vaguely resembles this:
Name Tutor Group
0 Joe Bloggs 6T1
We can use a function that takes in a number of arguments to write out paths at a target directory.
We can also check for path that we are creating if it exists or not, if not we can create it, if it does we pass over it.
def create_folders(dataframe, agg_columns, sub_folders,root_path):
p = Path(root_path)
series = dataframe[agg_columns].agg('-'.join,1)
for person in series:
trg_path = p.joinpath(person)
if not trg_path.is_dir():
trg_path.mkdir(parents=True)
for path in sub_folders:
if not trg_path.joinpath(path).is_dir():
trg_path.joinpath(path).mkdir()
create_folders(df,['Name','Tutor Group'], sub_folders, 'source')
Personally, I would also use this as a chance to learn how login
works if you don't already know. So that when you create these directories you have a log of what was created and when if any issues arise.
Upvotes: 1
Reputation: 101
I work on an csv example instead of an excel file, but can be modified for that purpose if desired.
for an input file:
Name,Tutor Group
A,1
B,1
C,1
D,2
E,2
import pandas as pd
import os
dirname = os.path.dirname(__file__)
file = f"{dirname}/input.csv"
df = pd.read_csv(file, sep=",")
group_person_dict = df.groupby("Tutor Group")["Name"].apply(list).to_dict()
# {
# "1": ["A", "B", "C"],
# "2": ["D","E"]
# }
# define dub folders
sub_folder_set = {
"AoW",
"CSP",
"Folder A",
"Folder B",
"Folder C",
"Folder D",
"Folder F",
"Folder E",
}
for group, people in group_person_dict.items():
g = str(group)
for p in people:
# obtain name+group concatanated string
folder_name = f"TutorGroup={g}-Person={p}"
# build main directory path, create if not exists
dir_path = os.path.join(dirname, folder_name)
if not os.path.exists(dir_path):
os.mkdir(dir_path)
# iterate ovr subfolders
for sbf in sub_folder_set:
# obtain sub directory path, create if not exists
subdir_path = os.path.join(dir_path, sbf)
if not os.path.exists(subdir_path):
os.mkdir(subdir_path)
Upvotes: 0
Reputation: 566
To read the xlxs file you can consider using pandas
library. After you're done with reading your file into pandas DataFrame, you can iterate through the rows and pick name and tutor group. After this step is completed, you can take built-in python lib os
and use os.makedir(path/to/dir/new_dir_name)
to create directory at specific path. Where directory new_dir_name would be a concatenation of values obtained for each DataFrame row.
Upvotes: 0