From 15b305739aa8431280fca43b84a5811b7e44dd51 Mon Sep 17 00:00:00 2001 From: Nimesh Ghelani Date: Sat, 23 Jan 2016 23:02:19 +0530 Subject: [PATCH] Safe file handling, closes #18 --- shuffle.py | 10 ++++------ 1 file changed, 4 insertions(+), 6 deletions(-) diff --git a/shuffle.py b/shuffle.py index 65ffcf6..90ef3bc 100755 --- a/shuffle.py +++ b/shuffle.py @@ -440,9 +440,8 @@ def remove_relatives(self, relative, filename): return fullPath def populate(self, filename): - f = open(filename, "rb") - data = f.readlines() - f.close() + with open(filename, 'rb') as f: + data = f.readlines() extension = os.path.splitext(filename)[1].lower() if extension == '.pls': @@ -529,9 +528,8 @@ def populate(self): self.lists.append(os.path.abspath(os.path.join(dirpath, filename))) def write_database(self): - f = open(os.path.join(self.base, "iPod_Control", "iTunes", "iTunesSD"), "wb") - f.write(self.tunessd.construct()) - f.close() + with open(os.path.join(self.base, "iPod_Control", "iTunes", "iTunesSD"), "wb") as f: + f.write(self.tunessd.construct()) # # Read all files from the directory