Skip to content

Instantly share code, notes, and snippets.

@btc
Created September 18, 2012 08:41
Show Gist options
  • Save btc/3742074 to your computer and use it in GitHub Desktop.
Save btc/3742074 to your computer and use it in GitHub Desktop.
copy files with rules
import difflib
import datetime
import os
import shutil
# where log is written
logFilename = 'log.txt'
# where torrents are located
torrentSource = 'C:\Users\Tchow\Downloads'
# where torrents need to go
torrentDestination = 'Y:'
# where downloads are located
fileDirectory = 'Y:\Downloads'
# where torrents are located
testTorrentSource = '/Users/brian/Desktop/test'
# where torrents need to go
testTorrentDestination = '/Users/brian/Desktop/testDest'
# where downloads are located
testFileDirectory = '/Volumes/Torrents/Downloads'
n = 1
while os.path.isfile(logFilename):
logFilename = 'log' + str(n) + '.txt'
n += 1
LOG = open(logFilename, "w")
LOG.write(str(datetime.datetime.now()) + '\n')
print 'log created: log.txt'
print 'Building list of torrents...'
if not os.path.isdir(testTorrentSource):
print testTorrentSource + ' does not exist'
for root, dirs, files in os.walk(testTorrentSource):
if root == testTorrentSource:
torrents = []
for file in files:
prefix = file[:-8]
suffix = file[-8:]
if suffix == '.torrent':
torrents.append(prefix)
#for dir in dirs:
print 'Matching torrents to dirs'
if not os.path.isdir(testFileDirectory):
print testFileDirectory + ' does not exist'
entries = os.listdir(testFileDirectory)
for e in entries:
possibilities = difflib.get_close_matches(e, torrents)
if possibilities == []:
LOG.write('no match for: ' + e + '\n')
for p in possibilities:
f = '/' + p + '.torrent'
srcFile = testTorrentSource + f
destFile = testTorrentDestination + f
if not os.path.isfile(destFile):
try:
shutil.copy2(srcFile, destFile)
print 'copied: ' + f
except IOError:
print 'file "' + f + '" already exists'
print 'done!'
LOG.close()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment