-
Description This solution would be more effitient for multiple writes, because of option "preopen".
Code class Solution: def __init__(self, data, filename='kumite776.txt', preopen=False): self.data = data self.filename = filename if preopen: self.file = open(self.filename, 'w') else: self.file = None def write_to_file(self): if not self.file: self.file = open(self.filename, 'w') self.file.write(self.data)
Preloaded Code def write_kumite_file(data): with open('kumite776.txt', 'w') as fo: fo.write(data) def open_file(): with open('kumite776.txt') as fo: file = fo.read() return file write_kumite_file('Hello Codewarz World!')
Test Cases import codewars_test as test from solution import Solution, open_file .describe("Example") def test_group(): .it("test case") def test_case(): original = open_file() Solution('hello world').write_to_file() new_file = open_file() test.assert_equals(original == new_file, False)
Output:
-
Code - class Solution:
def __init__(self, data):- def __init__(self, data, filename='kumite776.txt', preopen=False):
- self.data = data
self.filename = 'kumite776.txt'- self.filename = filename
- if preopen:
- self.file = open(self.filename, 'w')
- else:
- self.file = None
- def write_to_file(self):
with open(self.filename, 'w') as fo:fo.write(self.data)- if not self.file:
- self.file = open(self.filename, 'w')
- self.file.write(self.data)
- All
- {{group.name}} ({{group.count}})
This comment has been reported as {{ abuseKindText }}.
Show
This comment has been hidden. You can view it now .
This comment can not be viewed.
- |
- Reply
- Edit
- View Solution
- Expand 1 Reply Expand {{ comments?.length }} replies
- Collapse
- Remove
- Remove comment & replies
- Report
{{ fetchSolutionsError }}