Skip to content

Commit

Permalink
Now use tempfile library to create temporary directory
Browse files Browse the repository at this point in the history
  • Loading branch information
kcartier-wri committed Aug 23, 2024
1 parent ec5421d commit 621c1e2
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,8 @@ def test_geotiff_writing():
qgis_project_file = 'layers_for_br_lauro_de_freitas.qgs'

source_folder = os.path.dirname(__file__)
output_temp_folder = create_temp_folder('test_result_tif_files')
output_temp_folder = create_temp_folder('test_result_tif_files', True)

source_qgis_file = os.path.join(source_folder, qgis_project_file)
target_qgis_file = os.path.join(output_temp_folder, qgis_project_file)
shutil.copyfile(source_qgis_file, target_qgis_file)
Expand Down
25 changes: 12 additions & 13 deletions tools/general_tools.py
Original file line number Diff line number Diff line change
@@ -1,17 +1,16 @@
import platform
import os
import tempfile
import shutil

def create_temp_folder(sub_directory):
if platform.system() == 'Linux':
path = '/tmp/%s' % sub_directory
elif platform.system() == 'Windows':
localappdata_path = os.getenv("LOCALAPPDATA")
path = r'%s\Temp\%s' % (localappdata_path, sub_directory)
else:
raise ValueError('Method not implemented for this OS.')
def create_temp_folder(sub_directory_name: str, delete_existing_files: bool):
scratch_dir_name = tempfile.TemporaryDirectory().name
dirpath = os.path.dirname(scratch_dir_name)
temp_dir = os.path.join(dirpath, sub_directory_name)

if os.path.isdir(path) == False:
os.makedirs(path)

return path
if os.path.isdir(temp_dir) is False:
os.makedirs(temp_dir)
elif delete_existing_files is True:
shutil.rmtree(temp_dir)
os.makedirs(temp_dir)

return temp_dir

0 comments on commit 621c1e2

Please sign in to comment.