-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathWritingFiles.py
More file actions
31 lines (21 loc) · 792 Bytes
/
WritingFiles.py
File metadata and controls
31 lines (21 loc) · 792 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
25
26
27
28
29
30
31
################################################################################
# TITLE: Writing to Files in Python
# DESCRIPTION: How to detect and output errors to the user
################################################################################
def main():
# Call the openFile method and pass it Files/file1.txt
outputFile = input('Save as: ')
try:
writeFile(outputFile)
except:
print('Something is wrong with the filename you provided!')
def writeFile(filename):
writer = open(filename, 'w')
i=1
while i<=5:
writer.write('This is line {}\n'.format(i)) #\n means newline
i+=1
writer.close()
print('File created successfully!')
if __name__ == "__main__":
main()