eric6/Plugins/VcsPlugins/vcsGit/git.py

changeset 7785
9978016560ec
parent 7780
41420f82c0ac
child 7836
2f0d208b8137
--- a/eric6/Plugins/VcsPlugins/vcsGit/git.py	Tue Oct 13 19:02:26 2020 +0200
+++ b/eric6/Plugins/VcsPlugins/vcsGit/git.py	Wed Oct 14 17:50:39 2020 +0200
@@ -1400,10 +1400,9 @@
         if res:
             try:
                 # create a .gitignore file
-                ignore = open(ignoreName, "w")
-                ignore.write("\n".join(ignorePatterns))
-                ignore.write("\n")
-                ignore.close()
+                with open(ignoreName, "w") as ignore:
+                    ignore.write("\n".join(ignorePatterns))
+                    ignore.write("\n")
                 status = True
             except IOError:
                 status = False
@@ -1627,9 +1626,9 @@
             name2 = "{0} (rev. {1})".format(name, rev2)
         else:
             try:
-                f1 = open(name, "r", encoding="utf-8")
-                output2 = f1.read()
-                f1.close()
+                with open(name, "r", encoding="utf-8") as f1:
+                    output2 = f1.read()
+                    f1.close()
                 name2 = "{0} (Work)".format(name)
             except IOError:
                 E5MessageBox.critical(
@@ -2663,9 +2662,8 @@
             self.__lastReplayPath = os.path.dirname(fname)
             
             try:
-                f = open(fname, "w")
-                f.write(output)
-                f.close()
+                with open(fname, "w") as f:
+                    f.write(output)
             except (OSError, IOError) as err:
                 E5MessageBox.critical(
                     self.__ui,
@@ -3579,8 +3577,8 @@
         if not os.path.exists(cfgFile):
             # create an empty one
             try:
-                cfg = open(cfgFile, "w")
-                cfg.close()
+                with open(cfgFile, "w"):
+                    pass
             except IOError:
                 pass
         self.repoEditor = MiniEditor(cfgFile, "Properties")
@@ -3601,11 +3599,10 @@
                 firstName, lastName, email = (
                     "Firstname", "Lastname", "email_address")
             try:
-                f = open(cfgFile, "w")
-                f.write("[user]\n")
-                f.write("    name = {0} {1}\n".format(firstName, lastName))
-                f.write("    email = {0}\n".format(email))
-                f.close()
+                with open(cfgFile, "w") as f:
+                    f.write("[user]\n")
+                    f.write("    name = {0} {1}\n".format(firstName, lastName))
+                    f.write("    email = {0}\n".format(email))
             except (IOError, OSError):
                 # ignore these
                 pass
@@ -3886,9 +3883,8 @@
             return []
         
         try:
-            modulesFile = open(submodulesFile, "r")
-            contents = modulesFile.readlines()
-            modulesFile.close()
+            with open(submodulesFile, "r") as modulesFile:
+                contents = modulesFile.readlines()
         except OSError:
             # silently ignore them
             return []

eric ide

mercurial