open file with a context manager
authorJustin Findlay <redacted>
Thu, 15 Sep 2016 18:57:31 +0000 (12:57 -0600)
committerJustin Findlay <redacted>
Thu, 15 Sep 2016 18:59:44 +0000 (12:59 -0600)
xkcdpass/xkcd_password.py

index 8bd85542584f5a4a4452f960914335b28aa49b08..4903a571236a10534b4f36857c011f8d992430b4 100755 (executable)
@@ -114,14 +114,13 @@ def generate_wordlist(wordfile=None,
 
     # At this point wordfile is set
     wordfile = os.path.expanduser(wordfile)  # just to be sure
-    wlf = open(wordfile)
 
-    for line in wlf:
-        thisword = line.strip()
-        if regexp.match(thisword) is not None:
-            words.append(thisword)
-
-    wlf.close()
+    # read words from file into wordlist
+    with open(wordfile) as wlf:
+        for line in wlf:
+            thisword = line.strip()
+            if regexp.match(thisword) is not None:
+                words.append(thisword)
 
     return list(set(words))  # deduplicate, just in case
 
git clone https://git.99rst.org/PROJECT