utils.py 784 B

123456789101112131415161718192021222324252627282930313233
  1. #coding: utf-8
  2. #修正输入的文件路径,输入路径的标准格式:abc,不需要前后置的路径符号
  3. def FixFilePath(OutputPath):
  4. if callable(OutputPath):
  5. try:
  6. OutputPath=OutputPath()
  7. except Exception:
  8. OutputPath=""
  9. if len(OutputPath)>0:
  10. if OutputPath[-1]!="/":OutputPath="%s/" % OutputPath
  11. return OutputPath
  12. #在上传的文件名后面追加一个日期时间+随机,如abc.jpg--> abc_20120801202409.jpg
  13. def GenerateRndFilename(filename):
  14. import datetime
  15. import random
  16. from os.path import splitext
  17. f_name,f_ext=splitext(filename)
  18. return "%s_%s%s.%s" % (f_name, datetime.datetime.now().strftime("%Y%m%d_%H%M%S_"),random.randrange(10,99),f_ext)