-
-
Notifications
You must be signed in to change notification settings - Fork 344
/
Copy pathg.tempfile.html
79 lines (63 loc) · 2.63 KB
/
g.tempfile.html
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
<h2>DESCRIPTION</h2>
<em>g.tempfile</em>
creates a unique temporary file or directory (<b>-f</b>) and prints the path.
It is designed for shell scripts that need to use large temporary files.
GRASS provides a mechanism for temporary files or directories that does not
depend on /tmp. GRASS temporary files and directories are created in the
GRASS GIS database with the assumption that there will be enough space for
large files. The base directory is: $PROJECT/$MAPSET/.tmp/$HOSTNAME/
<p>
GRASS periodically removes temporary files that have been left behind
by programs that failed to remove them before terminating.
<p>
<em>g.tempfile</em> requires the user to provide an integer number,
usually a process-id (<b>pid</b>), which will be used as part of the file or
directory name of the resulting path, suffixed by a dot and the 0-indexed
number of temporary files with that PID.
Most Unix shells provide a way to get the
process id of the current shell. For /bin/sh and /bin/csh this is $$.
It is recommended that $$ be specified as the process-id for
<em>g.tempfile</em>.
<h2>EXAMPLE</h2>
For /bin/sh scripts the following syntax should be used:
<div class="code"><pre>
temp1=`g.tempfile pid=$$`
temp2=`g.tempfile pid=$$`
# Get the tempile path but do not create it
temp3=`g.tempfile -d pid=$$`
# Create a temporary directory
temp3=`g.tempfile -f pid=$$`
</pre></div>
For /bin/csh scripts, the following can be used:
<div class="code"><pre>
set temp1=`g.tempfile pid=$$`
set temp2=`g.tempfile pid=$$`
</pre></div>
<h2>NOTES</h2>
Each call to <em>g.tempfile</em>
creates a different (i.e. unique) name.
Although GRASS does eventually get around to removing
temporary files and directories that have been left behind,
the programmer should make every effort to remove these files.
They often get large and take up disk space. If you write /bin/sh
scripts, learn to use the /bin/sh <em>trap</em> command. If you
write /bin/csh scripts, learn to use the /bin/csh <em>onintr</em>
command. In Python, use an <em>atexit</em> procedure.
<p>
If the size of temporary files is not an issue, it is recommended
to use <i>NamedTemporaryFile</i> with a context manager to create a
temporary file in Python.<br>
In this example below, we open a temporary file for writing,
write something and then we can use it in another tool.
Once we do not need it anymore, we need to delete it ourselves.
<div class="code"><pre>
import tempfile
with tempfile.NamedTemporaryFile(mode="w", delete=False) as tmp_file:
file_path = tmp_file.name
tmp_file.write(...)
gs.try_remove(file_path)
</pre></div>
<h2>AUTHOR</h2>
Michael Shapiro,
U.S. Army Construction Engineering
Research Laboratory