Guest User

Untitled

a guest
Nov 21st, 2017
75
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 2.82 KB | None | 0 0
  1. import tempfile
  2. import itertools as IT
  3. import os
  4.  
  5. def uniquify(path, sep = ''):
  6. def name_sequence():
  7. count = IT.count()
  8. yield ''
  9. while True:
  10. yield '{s}{n:d}'.format(s = sep, n = next(count))
  11. orig = tempfile._name_sequence
  12. with tempfile._once_lock:
  13. tempfile._name_sequence = name_sequence()
  14. path = os.path.normpath(path)
  15. dirname, basename = os.path.split(path)
  16. filename, ext = os.path.splitext(basename)
  17. fd, filename = tempfile.mkstemp(dir = dirname, prefix = filename, suffix = ext)
  18. tempfile._name_sequence = orig
  19. return filename
  20.  
  21. print(uniquify('/tmp/file.pdf'))
  22.  
  23. import os
  24. index = ''
  25. while True:
  26. try:
  27. os.makedirs('../hi'+index)
  28. break
  29. except WindowsError:
  30. if index:
  31. index = '('+str(int(index[1:-1])+1)+')' # Append 1 to number in brackets
  32. else:
  33. index = '(1)'
  34. pass # Go and try create file again
  35.  
  36. def iter_incrementing_file_names(path):
  37. """
  38. Iterate incrementing file names. Start with path and add " (n)" before the
  39. extension, where n starts at 1 and increases.
  40.  
  41. :param path: Some path
  42. :return: An iterator.
  43. """
  44. yield path
  45. prefix, ext = os.path.splitext(path)
  46. for i in itertools.count(start=1, step=1):
  47. yield prefix + ' ({0})'.format(i) + ext
  48.  
  49.  
  50. def safe_open(path, mode):
  51. """
  52. Open path, but if it already exists, add " (n)" before the extension,
  53. where n is the first number found such that the file does not already
  54. exist.
  55.  
  56. Returns an open file handle. Make sure to close!
  57.  
  58. :param path: Some file name.
  59.  
  60. :return: Open file handle... be sure to close!
  61. """
  62. flags = os.O_CREAT | os.O_EXCL | os.O_WRONLY
  63.  
  64. if 'b' in mode and platform.system() == 'Windows':
  65. flags |= os.O_BINARY
  66.  
  67. for filename in iter_incrementing_file_names(path):
  68. try:
  69. file_handle = os.open(filename, flags)
  70. except OSError as e:
  71. if e.errno == errno.EEXIST:
  72. pass
  73. else:
  74. raise
  75. else:
  76. return os.fdopen(file_handle, mode)
  77.  
  78. # Example
  79. with safe_open("some_file.txt", "w") as fh:
  80. print("Hello", file=fh)
  81.  
  82. def increment_filename(fn):
  83. fn, extension = os.path.splitext(path)
  84.  
  85. n = 1
  86. yield fn + extension
  87. for n in itertools.count(start=1, step=1)
  88. yield '%s%d.%s' % (fn, n, extension)
  89.  
  90. for filename in increment_filename(original_filename):
  91. if not os.isfile(filename):
  92. break
  93.  
  94. import os
  95.  
  96. file_name = "file_name.txt"
  97. if os.path.isfile(file_name):
  98. expand = 1
  99. while True:
  100. expand += 1
  101. new_file_name = file_name.split(".txt")[0] + "(" + str(expand) + ")" + ".txt"
  102. if os.path.isfile(new_file_name):
  103. continue
  104. else:
  105. file_name = new_file_name
  106. break
Add Comment
Please, Sign In to add comment