Joe Smart
Joe Smart

Reputation: 763

Copying a file maintaining same folder structure

I am writing a backup script for documents I write daily.

Currently I walk through the destination folder and make a list of all files, then do the same for the source folder. If a file is in source but not in destination it copies it.

What I am struggling to work out is how to maintain the naming convention.

I store each document in a folder with its month, so my source dir has 12 folders named with the months. Inside these I have documents named "Daily stats 01092014" for example.

What I want is to maintain this filing structure in the backup folder.

When copying a file can I specify a folder based on its name?

Eg. if the document name has 09 as the 2 middle numbers in the datestamp then store it in september?

Upvotes: 0

Views: 114

Answers (1)

Neel
Neel

Reputation: 21243

Create dictionary with key as month number like

directory_mapping = {'01': 'jan', '02': 'feb', '03':'mar', '04':'apr', '05':'may' ... '12':'dec'}

then

filename = "Daily stats 01092014"
# Get folder name from filename by getting month number from filename
foldername = directory_mapping[filename[-6:-4]]

Upvotes: 1

Related Questions