Skip to content

Commit

Permalink
Use "with" when opening files (issue: #83).
Browse files Browse the repository at this point in the history
  • Loading branch information
alkisg committed Nov 21, 2018
1 parent 2090508 commit d54d5a4
Show file tree
Hide file tree
Showing 3 changed files with 14 additions and 20 deletions.
24 changes: 10 additions & 14 deletions epoptes/common/config.py
Expand Up @@ -37,9 +37,8 @@ def read_plain_file(filename):
if not os.path.isfile(filename):
return []
try:
file = open(filename, 'r')
contents = [x.strip() for x in file.readlines()]
file.close()
with open(filename, 'r') as file:
contents = [x.strip() for x in file.readlines()]
return contents
except (IOError, OSError) as exc:
LOG.e(exc)
Expand All @@ -50,8 +49,8 @@ def write_plain_file(filename, contents):
"""Write the contents string list to filename. Return True on success."""
try:
makedirs_for_file(filename)
file = open(filename, 'w')
file.write('\n'.join(contents))
with open(filename, 'w') as file:
file.write('\n'.join(contents))
return True
except (IOError, OSError) as exc:
LOG.e(exc)
Expand Down Expand Up @@ -88,9 +87,8 @@ def read_shell_file(filename):
if not os.path.isfile(filename):
return {}
try:
file = open(filename, 'r')
contents = file.read()
file.close()
with open(filename, 'r') as file:
contents = file.read()
contents = shlex.split(contents, True)
# TODO: maybe return at least all the valid pairs?
return dict(v.split('=') for v in contents)
Expand All @@ -106,9 +104,8 @@ def read_groups(filename):
if not os.path.isfile(filename):
return [], []
try:
file = open(filename)
data = json.loads(file.read())
file.close()
with open(filename) as file:
data = json.loads(file.read())
except (IOError, OSError) as exc:
LOG.e(exc)
return [], []
Expand Down Expand Up @@ -164,9 +161,8 @@ def save_groups(filename, model):
# Save the dict in JSON format
try:
makedirs_for_file(filename)
file = open(filename, 'w')
file.write(json.dumps(data, indent=2))
file.close()
with open(filename, 'w') as file:
file.write(json.dumps(data, indent=2))
except (IOError, OSError) as exc:
LOG.e(exc)

Expand Down
5 changes: 2 additions & 3 deletions epoptes/ui/gui.py
Expand Up @@ -286,9 +286,8 @@ def broadcast_screen(self, fullscreen=''):
pwd = ''.join(random.sample(
string.ascii_letters + string.digits, 8))
subprocess.call(['x11vnc', '-storepasswd', pwd, pwdfile])
file = open(pwdfile, 'rb')
pwd = file.read()
file.close()
with open(pwdfile, 'rb') as file:
pwd = file.read()
self.vncserver_port = self.find_unused_port()
self.vncserver_pwd = ''.join('\\%o' % c for c in pwd)
self.vncserver = subprocess.Popen(
Expand Down
5 changes: 2 additions & 3 deletions twisted/plugins/epoptesd.py
Expand Up @@ -45,9 +45,8 @@ def getContext(self):

def filter_bash(script):
"""Strip comments from client-functions, to save some bandwidth."""
file = open(script)
functions = file.readlines()
file.close()
with open(script) as file:
functions = file.readlines()
result = ''
for line in functions:
if line.strip() != '' and line.strip()[0] == '#':
Expand Down

0 comments on commit d54d5a4

Please sign in to comment.