Create temporary files on the same directory of target file

Change-Id: Ibfe9176ef56e5cd087dd7dc0ab6978d8e66b3845
This commit is contained in:
Federico Ressi 2019-11-12 18:42:07 +01:00
parent c0b0e6c3bc
commit 1df6547155
1 changed files with 2 additions and 1 deletions

View File

@ -42,11 +42,12 @@ def makedirs(name, mode=0o777, exist_ok=True):
@contextlib.contextmanager
def open_output_file(filename, mode='w', temp_dir=None, text=False):
basename = os.path.basename(filename)
dirname = os.path.dirname(filename)
prefix, suffix = os.path.splitext(basename)
prefix += '-'
temp_fd, temp_filename = tempfile.mkstemp(prefix=prefix,
suffix=suffix,
dir=temp_dir,
dir=temp_dir or dirname,
text=text)
try:
with os.fdopen(temp_fd, mode) as temp_stream: