HiveBrain v1.2.0
Get Started
← Back to all entries
patternpythonMinor

Backup files at the end of the day

Submitted by: @import:stackexchange-codereview··
0
Viewed 0 times
thefilesenddaybackup

Problem

This is my first attempt at writing code that moves files at the end of the day, from my local work folder to an network archive.

The program works but, any tips, tricks, or ideas to make this code better would be most appreciated.

import shutil, os, datetime, time, sys

os.system('TASKKILL /F /IM FlashRen.exe') 

newFolderName = datetime.datetime.now()
newFolder = newFolderName.strftime('%m%d%Y')

src = ('C:\\AEC Work\\' + newFolder)
dst = ('\\\qnap01\\Data Service\\AARON\\' +
           newFolderName.strftime('%Y\\') +
           newFolderName.strftime('%m. %B\\').upper() + newFolder)

for file in os.listdir(src):
    src_file = os.path.join(src, file)
    dst_file = os.path.join(dst, file)
    shutil.move(src, dst)

Solution

Doing multiple imports on the same line from different modules is frowned upon by PEP8, Python's official style-guide.

It also recommends using lower_case for variable names and not camelCase.

You don't need () around variables when assigning, src = 'C:\AEC Work\' + newFolder is enough.

Since you are already using os.path.join, which is a good thing, you should use it throughout.

You can use str.format to get "{:%m%d%Y}".format(datetime.datetime.now()) for the new folder name to make this slightly more compact.

import shutil
import os
import datetime
import time
import sys

os.system('TASKKILL /F /IM FlashRen.exe') 

now = datetime.datetime.now()
directory = "{:%m%d%Y}".format(now)

src = os.path.join('C:','AEC Work', directory)
backup_base = os.path.join('\qnap01', 'Data Service', 'AARON')
dst = os.path.join(backup_base, "{:%Y}".format(now),
                   "{:%m. %B}".format(now).upper(), directory)

for file in os.listdir(src):
    src_file = os.path.join(src, file)
    dst_file = os.path.join(dst, file)
    shutil.move(src, dst)

Code Snippets

import shutil
import os
import datetime
import time
import sys

os.system('TASKKILL /F /IM FlashRen.exe') 

now = datetime.datetime.now()
directory = "{:%m%d%Y}".format(now)

src = os.path.join('C:','AEC Work', directory)
backup_base = os.path.join('\qnap01', 'Data Service', 'AARON')
dst = os.path.join(backup_base, "{:%Y}".format(now),
                   "{:%m. %B}".format(now).upper(), directory)

for file in os.listdir(src):
    src_file = os.path.join(src, file)
    dst_file = os.path.join(dst, file)
    shutil.move(src, dst)

Context

StackExchange Code Review Q#145430, answer score: 3

Revisions (0)

No revisions yet.