forked from gaborbencsik/zerda-exam-python
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathsecond.py
More file actions
24 lines (20 loc) · 976 Bytes
/
second.py
File metadata and controls
24 lines (20 loc) · 976 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
# Create a function that takes a filename and a string as parameter,
# And writes the string got as second parameter into the file 10 times.
# If the writing succeeds, the function should return True.
# If any problem raises with the file output, the function should not break, but return False.
# Example: when called with the following two parameters: "tree.txt", "apple",
# the function should write "appleappleapple" to the file "tree.txt", and return True.
def string_to_file_writer(fileName, inputString):
if type(inputString) == str:
try:
inputFile = open(fileName, 'w')
newString = inputString * 10
inputFile.write(newString)
inputFile.close()
return True
except:
return False
else:
raise TypeError('only string type input accepted!')
print(string_to_file_writer('tree.txt', 'apple'))
print(string_to_file_writer('tree.txt', 1234)) # test by fake data