pathutil.py 756 B

1234567891011121314151617181920212223242526
  1. import os
  2. import tempfile
  3. def get_valid_filename(filename, dirname):
  4. current_file, counter = filename, 0
  5. while os.path.exists(os.path.join(dirname, current_file)):
  6. current_file = "%s_%d" % (filename, counter)
  7. counter += 1
  8. valid_path = os.path.join(dirname, current_file)
  9. os.mknod(valid_path)
  10. return valid_path
  11. def get_valid_tmpdir(name, tmp_dir):
  12. current_dir, counter = name, 0
  13. while os.path.exists(os.path.join(tmp_dir, current_dir)):
  14. current_dir = "%s_%d" % (name, counter)
  15. counter += 1
  16. os.mkdir(os.path.join(tmp_dir, current_dir))
  17. return os.path.join(tmp_dir, current_dir)
  18. def get_base_tmpdir(name):
  19. tmppath = tempfile.gettempdir()
  20. return get_valid_tmpdir(name, tmppath)