-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathPY0101EN-4-2-WriteFile.ipynb_FIRST_SUCCESS_WITH_LIMITED_KNOWLEDGE
1130 lines (1130 loc) · 34.8 KB
/
PY0101EN-4-2-WriteFile.ipynb_FIRST_SUCCESS_WITH_LIMITED_KNOWLEDGE
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
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
977
978
979
980
981
982
983
984
985
986
987
988
989
990
991
992
993
994
995
996
997
998
999
1000
{
"cells": [
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<center>\n",
" <img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/IDSNlogo.png\" width=\"300\" alt=\"cognitiveclass.ai logo\" />\n",
"</center>\n",
"\n",
"# Write and Save Files in Python\n",
"\n",
"Estimated time needed: **25** minutes\n",
"\n",
"## Objectives\n",
"\n",
"After completing this lab you will be able to:\n",
"\n",
"* Write to files using Python libraries\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2>Table of Contents</h2>\n",
"<div class=\"alert alert-block alert-info\" style=\"margin-top: 20px\">\n",
" <ul>\n",
" <li><a href=\"https://write/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01\">Writing Files</a></li>\n",
" <li><a href=\"https://append/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01\">Appending Files</a></li>\n",
" <li><a href=\"https://add/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01\">Additional File modes</a></li>\n",
" <li><a href=\"https://copy/?utm_medium=Exinfluencer&utm_source=Exinfluencer&utm_content=000026UJ&utm_term=10006555&utm_id=NA-SkillsNetwork-Channel-SkillsNetworkCoursesIBMDeveloperSkillsNetworkPY0101ENSkillsNetwork19487395-2021-01-01\">Copy a File</a></li>\n",
" </ul>\n",
"\n",
"</div>\n",
"\n",
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"write\">Writing Files</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can open a file object using the method <code>write()</code> to save the text file to a list. To write to a file, the mode argument must be set to **w**. Let’s write a file **Example2.txt** with the line: **“This is line A”**\n"
]
},
{
"cell_type": "code",
"execution_count": 7,
"metadata": {},
"outputs": [],
"source": [
"# Write line to file\n",
"exmp2 = 'Example2.txt'\n",
"with open(exmp2, 'w') as writefile:\n",
" writefile.write(\"This is line A\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can read the file to see if it worked:\n"
]
},
{
"cell_type": "code",
"execution_count": 8,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is line A\n"
]
}
],
"source": [
"# Read file\n",
"\n",
"with open(exmp2, 'r') as testwritefile:\n",
" print(testwritefile.read())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can write multiple lines:\n"
]
},
{
"cell_type": "code",
"execution_count": 14,
"metadata": {},
"outputs": [],
"source": [
"# Write lines to file\n",
"\n",
"with open(exmp2, 'w') as writefile:\n",
" writefile.write(\"This is line A\\n\")\n",
" writefile.write(\"This is line B\\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"The method <code>.write()</code> works similar to the method <code>.readline()</code>, except instead of reading a new line it writes a new line. The process is illustrated in the figure. The different colour coding of the grid represents a new line added to the file after each method call.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<img src=\"https://cf-courses-data.s3.us.cloud-object-storage.appdomain.cloud/IBMDeveloperSkillsNetwork-PY0101EN-SkillsNetwork/labs/Module%204/images/WriteLine.png\" width=\"500\" />\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can check the file to see if your results are correct\n"
]
},
{
"cell_type": "code",
"execution_count": 15,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is line A\n",
"This is line B\n",
"\n"
]
}
],
"source": [
"# Check whether write to file\n",
"\n",
"with open(exmp2, 'r') as testwritefile:\n",
" print(testwritefile.read())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We write a list to a **.txt** file as follows:\n"
]
},
{
"cell_type": "code",
"execution_count": 16,
"metadata": {},
"outputs": [
{
"data": {
"text/plain": [
"['This is line A\\n', 'This is line B\\n', 'This is line C\\n']"
]
},
"execution_count": 16,
"metadata": {},
"output_type": "execute_result"
}
],
"source": [
"# Sample list of text\n",
"\n",
"Lines = [\"This is line A\\n\", \"This is line B\\n\", \"This is line C\\n\"]\n",
"Lines"
]
},
{
"cell_type": "code",
"execution_count": 19,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is line A\n",
"This is line B\n",
"This is line C\n"
]
}
],
"source": [
"# Write the strings in the list to text file\n",
"\n",
"with open('Example2.txt', 'w') as writefile:\n",
" for line in Lines:\n",
" print(line.rstrip())\n",
" writefile.write(line.rstrip())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can verify the file is written by reading it and printing out the values:\n"
]
},
{
"cell_type": "code",
"execution_count": 20,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"This is line AThis is line BThis is line C\n"
]
}
],
"source": [
"# Verify if writing to file is successfully executed\n",
"\n",
"with open('Example2.txt', 'r') as testwritefile:\n",
" print(testwritefile.read())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"However, note that setting the mode to **w** overwrites all the existing data in the file.\n"
]
},
{
"cell_type": "code",
"execution_count": 21,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Overwrite\n",
"\n"
]
}
],
"source": [
"with open('Example2.txt', 'w') as writefile:\n",
" writefile.write(\"Overwrite\\n\")\n",
"with open('Example2.txt', 'r') as testwritefile:\n",
" print(testwritefile.read())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"<h2 id=\"Append\">Appending Files</h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can write to files without losing any of the existing data as follows by setting the mode argument to append: **a**. you can append a new line as follows:\n"
]
},
{
"cell_type": "code",
"execution_count": 22,
"metadata": {},
"outputs": [],
"source": [
"# Write a new line to text file\n",
"\n",
"with open('Example2.txt', 'a') as testwritefile:\n",
" testwritefile.write(\"This is line C\\n\")\n",
" testwritefile.write(\"This is line D\\n\")\n",
" testwritefile.write(\"This is line E\\n\")"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"You can verify the file has changed by running the following cell:\n"
]
},
{
"cell_type": "code",
"execution_count": 48,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"<class '_io.TextIOWrapper'>\n",
"<class 'str'>\n",
"\n"
]
}
],
"source": [
"# Verify if the new line is in the text file\n",
"\n",
"\n",
"with open('Example2.txt', 'r') as testwritefile:\n",
" x = testwritefile\n",
" print(type(x))\n",
" print(type(testwritefile.read()))\n",
" print(testwritefile.read())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n",
"<h2 id=\"add\">Additional modes</h2> \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"It's fairly ineffecient to open the file in **a** or **w** and then reopening it in **r** to read any lines. Luckily we can access the file in the following modes:\n",
"\n",
"* **r+** : Reading and writing. Cannot truncate the file.\n",
"* **w+** : Writing and reading. Truncates the file.\n",
"* **a+** : Appending and Reading. Creates a new file, if none exists.\n",
"\n",
"You dont have to dwell on the specifics of each mode for this lab.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's try out the **a+** mode:\n"
]
},
{
"cell_type": "code",
"execution_count": 45,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"\n"
]
}
],
"source": [
"with open('Example2.txt', 'a+') as testwritefile:\n",
" testwritefile.write(\"This is line E\\n\")\n",
" print(testwritefile.read())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"There were no errors but <code>read()</code> also did not output anything. This is because of our location in the file.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Most of the file methods we've looked at work in a certain location in the file. <code>.write() </code> writes at a certain location in the file. <code>.read()</code> reads at a certain location in the file and so on. You can think of this as moving your pointer around in the notepad to make changes at specific location.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Opening the file in **w** is akin to opening the .txt file, moving your cursor to the beginning of the text file, writing new text and deleting everything that follows.\n",
"Whereas opening the file in **a** is similiar to opening the .txt file, moving your cursor to the very end and then adding the new pieces of text. <br>\n",
"It is often very useful to know where the 'cursor' is in a file and be able to control it. The following methods allow us to do precisely this -\n",
"\n",
"* <code>.tell()</code> - returns the current position in bytes\n",
"* <code>.seek(offset,from)</code> - changes the position by 'offset' bytes with respect to 'from'. From can take the value of 0,1,2 corresponding to beginning, relative to current position and end\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now lets revisit **a+**\n"
]
},
{
"cell_type": "code",
"execution_count": 55,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Initial Location: 70\n",
"<class 'int'>\n",
"Read nothing\n",
"\n",
"New Location : 0\n",
"Overwrite\n",
"This is line C\n",
"This is line D\n",
"This is line E\n",
"This is line E\n",
"\n",
"Location after read: 70\n"
]
}
],
"source": [
"with open('Example2.txt', 'a+') as testwritefile:\n",
" print(\"Initial Location: {}\".format(testwritefile.tell()))\n",
" print(type(testwritefile.tell()))\n",
" \n",
" data = testwritefile.read()\n",
" if (not data): #empty strings return false in python\n",
" print('Read nothing') \n",
" else: \n",
" print(testwritefile.read())\n",
" \n",
" testwritefile.seek(0,0) # move 0 bytes from beginning.\n",
" \n",
" print(\"\\nNew Location : {}\".format(testwritefile.tell()))\n",
" data = testwritefile.read()\n",
" if (not data): \n",
" print('Read nothing') \n",
" else: \n",
" print(data)\n",
" \n",
" print(\"Location after read: {}\".format(testwritefile.tell()) )"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Finally, a note on the difference between **w+** and **r+**. Both of these modes allow access to read and write methods, however, opening a file in **w+** overwrites it and deletes all pre-existing data. <br>\n",
"To work with a file on existing data, use **r+** and **a+**. While using **r+**, it can be useful to add a <code>.truncate()</code> method at the end of your data. This will reduce the file to your data and delete everything that follows. <br>\n",
"In the following code block, Run the code as it is first and then run it with the <code>.truncate()</code>.\n"
]
},
{
"cell_type": "code",
"execution_count": 58,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Line 1\n",
"Line 2\n",
"Line 3\n",
"finished\n",
"\n"
]
}
],
"source": [
"with open('Example2.txt', 'r+') as testwritefile:\n",
" data = testwritefile.readlines()\n",
" testwritefile.seek(0,0) #write at beginning of file\n",
" \n",
" testwritefile.write(\"Line 1\" + \"\\n\")\n",
" testwritefile.write(\"Line 2\" + \"\\n\")\n",
" testwritefile.write(\"Line 3\" + \"\\n\")\n",
" testwritefile.write(\"finished\\n\")\n",
" #Uncomment the line below\n",
" #testwritefile.truncate()\n",
" testwritefile.seek(0,0)\n",
" print(testwritefile.read())\n",
" "
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2 id=\"copy\">Copy a File</h2> \n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Let's copy the file **Example2.txt** to the file **Example3.txt**:\n"
]
},
{
"cell_type": "code",
"execution_count": 60,
"metadata": {},
"outputs": [],
"source": [
"# Copy file to another\n",
"\n",
"with open('Example2.txt','r') as readfile:\n",
" with open('Example3.txt','w') as writefile:\n",
" for line in readfile:\n",
" writefile.write(line)"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"We can read the file to see if everything works:\n"
]
},
{
"cell_type": "code",
"execution_count": 61,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Line 1\n",
"Line 2\n",
"Line 3\n",
"finished\n",
"\n"
]
}
],
"source": [
"# Verify if the copy is successfully executed\n",
"\n",
"with open('Example3.txt','r') as testwritefile:\n",
" print(testwritefile.read())"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"After reading files, we can also write data into files and save them in different file formats like **.txt, .csv, .xls (for excel files) etc**. You will come across these in further examples\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Now go to the directory to ensure the **.txt** file exists and contains the summary data that we wrote.\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<hr>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"<h2> Exercise </h2>\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Your local university's Raptors fan club maintains a register of its active members on a .txt document. Every month they update the file by removing the members who are not active. You have been tasked with automating this with your Python skills. <br>\n",
"Given the file currentMem, Remove each member with a 'no' in their Active coloumn. Keep track of each of the removed members and append them to the exMem file. Make sure the format of the original files in preserved. (*Hint: Do this by reading/writing whole lines and ensuring the header remains* ) <br>\n",
"Run the code block below prior to starting the exercise. The skeleton code has been provided for you, Edit only the cleanFiles function.\n"
]
},
{
"cell_type": "code",
"execution_count": 62,
"metadata": {},
"outputs": [],
"source": [
"#Run this prior to starting the exercise\n",
"from random import randint as rnd\n",
"\n",
"memReg = 'members.txt'\n",
"exReg = 'inactive.txt'\n",
"fee =('yes','no')\n",
"\n",
"def genFiles(current,old):\n",
" with open(current,'w+') as writefile: \n",
" writefile.write('Membership No Date Joined Active \\n')\n",
" data = \"{:^13} {:<11} {:<6}\\n\"\n",
"\n",
" for rowno in range(20):\n",
" date = str(rnd(2015,2020))+ '-' + str(rnd(1,12))+'-'+str(rnd(1,25))\n",
" writefile.write(data.format(rnd(10000,99999),date,fee[rnd(0,1)]))\n",
"\n",
"\n",
" with open(old,'w+') as writefile: \n",
" writefile.write('Membership No Date Joined Active \\n')\n",
" data = \"{:^13} {:<11} {:<6}\\n\"\n",
" for rowno in range(3):\n",
" date = str(rnd(2015,2020))+ '-' + str(rnd(1,12))+'-'+str(rnd(1,25))\n",
" writefile.write(data.format(rnd(10000,99999),date,fee[1]))\n",
"\n",
"\n",
"genFiles(memReg,exReg)\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Start your solution below:\n"
]
},
{
"cell_type": "code",
"execution_count": 74,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Active Members: \n",
"\n",
"\n",
"Membership No Date Joined Active \n",
" 57230 2018-12-17 yes \n",
" 96826 2020-8-14 yes \n",
" 96881 2016-4-2 yes \n",
" 91181 2019-4-8 yes \n",
" 41739 2020-12-14 yes \n",
" 39757 2019-2-9 yes \n",
" 96506 2017-5-9 yes \n",
" 26352 2017-6-24 yes \n",
" 29190 2017-10-22 yes \n",
" 34690 2020-1-10 yes \n",
" 66818 2016-8-10 yes \n",
"\n",
"Inactive Members: \n",
"\n",
"\n",
"Membership No Date Joined Active \n",
" 94516 2018-3-12 no \n",
" 29316 2015-4-3 no \n",
" 35013 2016-4-18 no \n",
" 25727 2016-4-11 no \n",
" 66995 2017-2-4 no \n",
" 58495 2017-6-20 no \n",
" 81517 2017-11-3 no \n",
" 94022 2016-9-4 no \n",
" 42896 2018-7-9 no \n",
" 50625 2020-5-1 no \n",
" 21159 2016-10-18 no \n",
" 58296 2017-10-17 no \n",
"\n",
"Preserved memReg file :\n",
"\n",
"Membership No Date Joined Active \n",
" 57230 2018-12-17 yes \n",
" 25727 2016-4-11 no \n",
" 66995 2017-2-4 no \n",
" 96826 2020-8-14 yes \n",
" 58495 2017-6-20 no \n",
" 96881 2016-4-2 yes \n",
" 81517 2017-11-3 no \n",
" 94022 2016-9-4 no \n",
" 91181 2019-4-8 yes \n",
" 41739 2020-12-14 yes \n",
" 39757 2019-2-9 yes \n",
" 42896 2018-7-9 no \n",
" 50625 2020-5-1 no \n",
" 21159 2016-10-18 no \n",
" 96506 2017-5-9 yes \n",
" 26352 2017-6-24 yes \n",
" 29190 2017-10-22 yes \n",
" 58296 2017-10-17 no \n",
" 34690 2020-1-10 yes \n",
" 66818 2016-8-10 yes \n",
"\n",
"Preserved exReg file :\n",
"\n",
"Membership No Date Joined Active \n",
" 94516 2018-3-12 no \n",
" 29316 2015-4-3 no \n",
" 35013 2016-4-18 no \n",
"\n"
]
}
],
"source": [
"\n",
"def cleanFiles(currentMem,exMem):\n",
" '''\n",
" currentMem: File containing list of current members\n",
" exMem: File containing list of old members\n",
" \n",
" Removes all rows from currentMem containing 'no' and appends them to exMem\n",
" '''\n",
" for line in currentMem:\n",
" if 'no' in line:\n",
" exMem.append(line)\n",
" currentMem.remove(line)\n",
"\n",
"\n",
"# Code to help you see the files\n",
"# Leave as is\n",
"memReg = 'members.txt'\n",
"exReg = 'inactive.txt'\n",
"cleanFiles(memReg,exReg)\n",
"\n",
"empt_list = list()\n",
"headers = \"Membership No Date Joined Active \\n\"\n",
"with open(memReg,'r') as readFile:\n",
" with open('new_currentMem.txt', 'w') as writeFile:\n",
" for line in readFile:\n",
" if 'no' not in line:\n",
" writeFile.write(line)\n",
" else:\n",
" empt_list.append(line)\n",
" \n",
"with open('new_currentMem.txt', 'r') as readFile1:\n",
" print(\"Active Members: \\n\\n\")\n",
" print(readFile1.read())\n",
" \n",
"with open(exReg,'r') as readFile:\n",
" with open('new_exMem.txt', 'w') as writeFile:\n",
" for line in readFile:\n",
" writeFile.write(line)\n",
"with open('new_exMem.txt', 'a') as appendingFile:\n",
" for line in empt_list:\n",
" appendingFile.write(line)\n",
"\n",
"with open('new_exMem.txt', 'r') as readFile2:\n",
" print(\"Inactive Members: \\n\\n\")\n",
" print(readFile2.read())\n",
"\n",
"print(\"Preserved memReg file :\\n\")\n",
"with open(memReg, 'r') as readfile:\n",
" print(readfile.read())\n",
"print(\"Preserved exReg file :\\n\")\n",
"with open(exReg, 'r') as readfile:\n",
" print(readfile.read())\n"
]
},
{
"cell_type": "code",
"execution_count": 76,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Active Members: \n",
"\n",
"\n",
"Membership No Date Joined Active \n",
" 57230 2018-12-17 yes \n",
" 25727 2016-4-11 no \n",
" 66995 2017-2-4 no \n",
" 96826 2020-8-14 yes \n",
" 58495 2017-6-20 no \n",
" 96881 2016-4-2 yes \n",
" 81517 2017-11-3 no \n",
" 94022 2016-9-4 no \n",
" 91181 2019-4-8 yes \n",
" 41739 2020-12-14 yes \n",
" 39757 2019-2-9 yes \n",
" 42896 2018-7-9 no \n",
" 50625 2020-5-1 no \n",
" 21159 2016-10-18 no \n",
" 96506 2017-5-9 yes \n",
" 26352 2017-6-24 yes \n",
" 29190 2017-10-22 yes \n",
" 58296 2017-10-17 no \n",
" 34690 2020-1-10 yes \n",
" 66818 2016-8-10 yes \n",
"\n",
"Inactive Members: \n",
"\n",
"\n",
"Membership No Date Joined Active \n",
" 94516 2018-3-12 no \n",
" 29316 2015-4-3 no \n",
" 35013 2016-4-18 no \n",
"\n"
]
}
],
"source": [
"# original code written just above\n",
"\n",
"def cleanFiles(currentMem,exMem):\n",
" '''\n",
" currentMem: File containing list of current members\n",
" exMem: File containing list of old members\n",
" \n",
" Removes all rows from currentMem containing 'no' and appends them to exMem\n",
" '''\n",
" pass\n",
"\n",
"# Code to help you see the files\n",
"# Leave as is\n",
"memReg = 'members.txt'\n",
"exReg = 'inactive.txt'\n",
"cleanFiles(memReg,exReg)\n",
"\n",
"\n",
"headers = \"Membership No Date Joined Active \\n\"\n",
"with open(memReg,'r') as readFile:\n",
" print(\"Active Members: \\n\\n\")\n",
" print(readFile.read())\n",
" \n",
"with open(exReg,'r') as readFile:\n",
" print(\"Inactive Members: \\n\\n\")\n",
" print(readFile.read())\n",
" \n",
" \n"
]
},
{
"cell_type": "code",
"execution_count": 77,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Active Members: \n",
"\n",
"\n",
"Membership No Date Joined Active \n",
" 57230 2018-12-17 yes \n",
" 96826 2020-8-14 yes \n",
" 96881 2016-4-2 yes \n",
" 91181 2019-4-8 yes \n",
" 41739 2020-12-14 yes \n",
" 39757 2019-2-9 yes \n",
" 96506 2017-5-9 yes \n",
" 26352 2017-6-24 yes \n",
" 29190 2017-10-22 yes \n",
" 34690 2020-1-10 yes \n",
" 66818 2016-8-10 yes \n",
"\n",
"Inactive Members: \n",
"\n",
"\n",
"Membership No Date Joined Active \n",
" 94516 2018-3-12 no \n",
" 29316 2015-4-3 no \n",
" 35013 2016-4-18 no \n",
" 25727 2016-4-11 no \n",
" 66995 2017-2-4 no \n",
" 58495 2017-6-20 no \n",
" 81517 2017-11-3 no \n",
" 94022 2016-9-4 no \n",
" 42896 2018-7-9 no \n",
" 50625 2020-5-1 no \n",
" 21159 2016-10-18 no \n",
" 58296 2017-10-17 no \n",
"\n"
]
}
],
"source": [
"def cleanFiles(currentMem,exMem):\n",
" with open(currentMem,'r+') as writeFile: \n",
" with open(exMem,'a+') as appendFile:\n",
" #get the data\n",
" writeFile.seek(0)\n",
" members = writeFile.readlines()\n",
" #remove header\n",
" header = members[0]\n",
" members.pop(0)\n",
" \n",
" inactive = [member for member in members if ('no' in member)]\n",
" '''\n",
" The above is the same as \n",
"\n",
" for member in members:\n",
" if 'no' in member:\n",
" inactive.append(member)\n",
" '''\n",
" #go to the beginning of the write file\n",
" writeFile.seek(0) \n",
" writeFile.write(header)\n",
" for member in members:\n",
" if (member in inactive):\n",
" appendFile.write(member)\n",
" else:\n",
" writeFile.write(member) \n",
" writeFile.truncate()\n",
" \n",
"memReg = 'members.txt'\n",
"exReg = 'inactive.txt'\n",
"cleanFiles(memReg,exReg)\n",
"\n",
"# code to help you see the files\n",
"\n",
"headers = \"Membership No Date Joined Active \\n\"\n",
"\n",
"with open(memReg,'r') as readFile:\n",
" print(\"Active Members: \\n\\n\")\n",
" print(readFile.read())\n",
" \n",
"with open(exReg,'r') as readFile:\n",
" print(\"Inactive Members: \\n\\n\")\n",
" print(readFile.read())\n"
]
},
{
"cell_type": "markdown",
"metadata": {},
"source": [
"Run the following to verify your code:\n"
]
},
{
"cell_type": "code",
"execution_count": 75,
"metadata": {},
"outputs": [
{
"name": "stdout",
"output_type": "stream",
"text": [
"Inactive members in file\n",
"Test Failed\n"
]
}
],
"source": [
"def testMsg(passed):\n",
" if passed:\n",
" return 'Test Passed'\n",
" else :\n",
" return 'Test Failed'\n",
"\n",
"testWrite = \"testWrite.txt\"\n",
"testAppend = \"testAppend.txt\" \n",
"passed = True\n",
"\n",
"genFiles(testWrite,testAppend)\n",
"\n",
"with open(testWrite,'r') as file:\n",
" ogWrite = file.readlines()\n",
"\n",
"with open(testAppend,'r') as file:\n",
" ogAppend = file.readlines()\n",
"\n",
"try:\n",
" cleanFiles(testWrite,testAppend)\n",
"except:\n",
" print('Error')\n",
"\n",
"with open(testWrite,'r') as file:\n",
" clWrite = file.readlines()\n",
"\n",
"with open(testAppend,'r') as file:\n",
" clAppend = file.readlines()\n",
" \n",
"# checking if total no of rows is same, including headers\n",
"\n",
"if (len(ogWrite) + len(ogAppend) != len(clWrite) + len(clAppend)):\n",
" print(\"The number of rows do not add up. Make sure your final files have the same header and format.\")\n",
" passed = False\n",
" \n",
"for line in clWrite:\n",
" if 'no' in line:\n",
" passed = False\n",
" print(\"Inactive members in file\")\n",
" break\n",
" else:\n",