Skip to content

Instantly share code, notes, and snippets.

@baali
Created May 13, 2012 06:03
Show Gist options
  • Save baali/2686411 to your computer and use it in GitHub Desktop.
Save baali/2686411 to your computer and use it in GitHub Desktop.
To check for duplicate files
if bool(fileName):
flag = 1
filePath = os.path.join(detach_dir, 'attachments', fileName)
if not os.path.isfile(filePath) :
print fileName
fp = open(filePath, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
else:
tmpPath = os.path.join(detach_dir, 'attachments', fileName+'tmp')
fp = open(tmpPath, 'wb')
fp.write(part.get_payload(decode=True))
fp.close()
# To check against all file name variants
fileCounter = 1
while not filecmp.cmp(filePath, tmpPath):
newName = fileName.split('.')
newName = '.'.join(newName[:-1]) + '.' + str(fileCounter) + '.' + newName[-1]
newPath = os.path.join(detach_dir, 'attachments', newName)
if os.path.isfile(newPath):
filePath = newPath
fileCounter += 1
else:
os.rename(tmpPath, newPath)
break
if filecmp.cmp(filePath, tmpPath):
os.remove(tmpPath)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment