Skip to content

Commit 679aa8f

Browse files
joarkarthiknadig
authored andcommitted
printEnvVariablesToFile.py overwrites itself if invoked without arguments
I was the command in my terminal when launching the editor, was curious to see what it did, so I ran it again without arguments, expecting to get a `--help`-like message, instead it ended up overwriting itself. This change changes the script's default behavior (default being the invocation without arguments) from overwriting itself to throwing an exception about a missing output file argument.
1 parent 5cec0e0 commit 679aa8f

File tree

1 file changed

+7
-2
lines changed

1 file changed

+7
-2
lines changed

python_files/printEnvVariablesToFile.py

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,8 +4,13 @@
44
import os
55
import sys
66

7-
# Last argument is the target file into which we'll write the env variables line by line.
8-
output_file = sys.argv[-1]
7+
8+
# Prevent overwriting itself, since sys.argv[0] is the path to this file
9+
if len(sys.argv) > 1:
10+
# Last argument is the target file into which we'll write the env variables line by line.
11+
output_file = sys.argv[-1]
12+
else:
13+
raise ValueError("Missing output file argument")
914

1015
with open(output_file, "w") as outfile: # noqa: PTH123
1116
for key, val in os.environ.items():

0 commit comments

Comments
 (0)