Reputation: 557
I have code with this logic: Make the directory from column "Folder_Name_to_create" if it exists. I am trying to add to this newly created directories a STATIC set of subfolders with its contents.
Getting subfolders to be copied to the newly created folder:
subfolders_to_create = []
for entry in pl_Template_Dir.glob('*'):
if entry.is_dir():
subfolders_to_create.append(entry.name)
Code that creates new directory if exists:
for folder in pl_dest['Folder_Name_to_create']:
try:
pathlib.Path(folder).mkdir()
except FileExistsError:
pass
What I am trying:
for sd in subfolders_to_create:
(pl_dest / sd).mkdir()
error: AttributeError: 'DataFrame' object has no attribute 'mkdir'
pl_dest
dataframe:
```
Folder_Name_to_create
0 O:\Stack\Over\Flow\2010
1 O:\Stack\Over\Flow\2011
```
Im having trouble putting this together. I want to create new directories if they exist and bring in the the Template Subfolders and contents to each newly created directory.
Upvotes: 0
Views: 52
Reputation: 56
You are calling mkdir
on DataFrame, which has no such method. Although pl_dest / sd
appends subdirectories to all parent folders in df, the call still returns a dataframe. You should make individual call on every element of the new df. It would look something like this:
for subfolder in subfolders_to_create:
for folder_to_create in pl_dest['Folder_Name_to_create'] / subfolder:
# exist_ok=True instead of try/catch
folder_to_create.mkdir(exist_ok=True, parents=True)
Edit:
To copy subfolders with contents use shutils. In this example subfolders_to_create contains paths, not only relative names.
import shutils
for src_dir in subfolders_to_create:
for dst_dir in pl_dest['Folder_Name_to_create']:
shutils.copytree(src_dir, dst_dir)
Upvotes: 1