|
556 | 556 | "cell_type": "markdown",
|
557 | 557 | "metadata": {},
|
558 | 558 | "source": [
|
559 |
| - "### Reading from Files" |
| 559 | + "### Reading from files" |
560 | 560 | ]
|
561 | 561 | },
|
562 | 562 | {
|
|
586 | 586 | "source": [
|
587 | 587 | "# Let's have a look at a data file that we prepared earlier\n",
|
588 | 588 | "\n",
|
589 |
| - "with open('datafiles/bright.txt') as filepath: # the file is stored in a subdirectory called 'datafiles'\n", |
590 |
| - " file_data = filepath.read() # all the file is read \n", |
| 589 | + "with open('datafiles/bright.txt') as f: # the file is stored in a subdirectory called 'datafiles'\n", |
| 590 | + " file_data = f.read() # all the file is read \n", |
591 | 591 | "print(file_data)"
|
592 | 592 | ]
|
593 | 593 | },
|
|
679 | 679 | },
|
680 | 680 | "outputs": [],
|
681 | 681 | "source": [
|
682 |
| - "with open('datafiles/bright.txt') as filepath: \n", |
683 |
| - " file_data = filepath.readlines() \n", |
| 682 | + "with open('datafiles/bright.txt') as f: \n", |
| 683 | + " file_data = f.readlines() \n", |
684 | 684 | "print(file_data)"
|
685 | 685 | ]
|
686 | 686 | },
|
|
713 | 713 | "metadata": {},
|
714 | 714 | "outputs": [],
|
715 | 715 | "source": [
|
716 |
| - "with open('datafiles/bright.txt') as filepath: \n", |
717 |
| - " lines = filepath.readlines() \n", |
| 716 | + "with open('datafiles/bright.txt') as f: \n", |
| 717 | + " lines = f.readlines() \n", |
718 | 718 | "\n",
|
719 | 719 | "for line in lines:\n",
|
720 | 720 | " print(line)\n",
|
|
727 | 727 | "source": [
|
728 | 728 | "##### Note: we'll play more with reading files in the exercises and in the future tutorials."
|
729 | 729 | ]
|
| 730 | + }, |
| 731 | + { |
| 732 | + "cell_type": "markdown", |
| 733 | + "metadata": {}, |
| 734 | + "source": [ |
| 735 | + "### Writing to files" |
| 736 | + ] |
| 737 | + }, |
| 738 | + { |
| 739 | + "cell_type": "markdown", |
| 740 | + "metadata": {}, |
| 741 | + "source": [ |
| 742 | + "Besides reading to a file, you might also need to save information to file. For the sake of simplicity, let us assume you are trying to save data to a .txt file." |
| 743 | + ] |
| 744 | + }, |
| 745 | + { |
| 746 | + "cell_type": "markdown", |
| 747 | + "metadata": {}, |
| 748 | + "source": [ |
| 749 | + "You have two options to write data into a file:\n", |
| 750 | + "- write() : Inserts a string in a single line in the file.\n", |
| 751 | + "- writelines() : Inserts a list of string, one string per single line in the file." |
| 752 | + ] |
| 753 | + }, |
| 754 | + { |
| 755 | + "cell_type": "code", |
| 756 | + "execution_count": null, |
| 757 | + "metadata": {}, |
| 758 | + "outputs": [], |
| 759 | + "source": [ |
| 760 | + "# Print file contents before modifications\n", |
| 761 | + "print('### File content BEFORE modifications ###\\n')\n", |
| 762 | + "with open('datafiles/bright_modified.txt') as f: \n", |
| 763 | + " lines = f.readlines() \n", |
| 764 | + "for line in lines:\n", |
| 765 | + " print(line)" |
| 766 | + ] |
| 767 | + }, |
| 768 | + { |
| 769 | + "cell_type": "code", |
| 770 | + "execution_count": null, |
| 771 | + "metadata": { |
| 772 | + "scrolled": true |
| 773 | + }, |
| 774 | + "outputs": [], |
| 775 | + "source": [ |
| 776 | + "# Modify file\n", |
| 777 | + "new_lines = [\n", |
| 778 | + " \"If life seems jolly rotten \\n\",\n", |
| 779 | + " \"There's something you've forgotten \\n\",\n", |
| 780 | + " \"And that's to laugh and smile and dance and sing \\n\",\n", |
| 781 | + " \"When you're feeling in the dumps \\n\",\n", |
| 782 | + " \"Don't be silly, chumps \\n\",\n", |
| 783 | + " \"Just purse your lips and whistle - that's the thing \\n\",\n", |
| 784 | + " \"And always look on the bright side of life \\n\",\n", |
| 785 | + " \"Always look on the light side of life \\n\",\n", |
| 786 | + "]\n", |
| 787 | + "\n", |
| 788 | + "with open('datafiles/bright_modified.txt') as f: \n", |
| 789 | + " f.writelines(new_lines) " |
| 790 | + ] |
| 791 | + }, |
| 792 | + { |
| 793 | + "cell_type": "markdown", |
| 794 | + "metadata": {}, |
| 795 | + "source": [ |
| 796 | + "The above failed to run because you need to open the file in a specific 'write' mode to be able to write to it." |
| 797 | + ] |
| 798 | + }, |
| 799 | + { |
| 800 | + "cell_type": "code", |
| 801 | + "execution_count": null, |
| 802 | + "metadata": {}, |
| 803 | + "outputs": [], |
| 804 | + "source": [ |
| 805 | + "# Modify file\n", |
| 806 | + "new_lines = [\n", |
| 807 | + " \"If life seems jolly rotten \\n\",\n", |
| 808 | + " \"There's something you've forgotten \\n\",\n", |
| 809 | + " \"And that's to laugh and smile and dance and sing \\n\",\n", |
| 810 | + " \"When you're feeling in the dumps \\n\",\n", |
| 811 | + " \"Don't be silly, chumps \\n\",\n", |
| 812 | + " \"Just purse your lips and whistle - that's the thing \\n\",\n", |
| 813 | + " \"And always look on the bright side of life \\n\",\n", |
| 814 | + " \"Always look on the light side of life \\n\",\n", |
| 815 | + "]\n", |
| 816 | + "\n", |
| 817 | + "with open('datafiles/bright_modified.txt', 'w') as f: \n", |
| 818 | + " f.writelines(new_lines) " |
| 819 | + ] |
| 820 | + }, |
| 821 | + { |
| 822 | + "cell_type": "code", |
| 823 | + "execution_count": null, |
| 824 | + "metadata": { |
| 825 | + "scrolled": false |
| 826 | + }, |
| 827 | + "outputs": [], |
| 828 | + "source": [ |
| 829 | + "# Print file contents after modifications \n", |
| 830 | + "print('### File content AFTER modifications ###\\n')\n", |
| 831 | + "with open('datafiles/bright_modified.txt') as f: \n", |
| 832 | + " lines = f.readlines() \n", |
| 833 | + "for line in lines:\n", |
| 834 | + " print(line)" |
| 835 | + ] |
| 836 | + }, |
| 837 | + { |
| 838 | + "cell_type": "markdown", |
| 839 | + "metadata": {}, |
| 840 | + "source": [ |
| 841 | + "Oups! Oh, no! We have overwritten the file! \n", |
| 842 | + "<br>\n", |
| 843 | + "<br>\n", |
| 844 | + "Let's see how to correctly append a new line to the end of the file without overwriting the existing content.\n", |
| 845 | + "<br>\n", |
| 846 | + "<br>\n", |
| 847 | + "In order to do that, we need to open the file in the 'append' mode." |
| 848 | + ] |
| 849 | + }, |
| 850 | + { |
| 851 | + "cell_type": "code", |
| 852 | + "execution_count": null, |
| 853 | + "metadata": {}, |
| 854 | + "outputs": [], |
| 855 | + "source": [ |
| 856 | + "# Modify file\n", |
| 857 | + "new_lines = [\n", |
| 858 | + " \"TEST NO OVERWRITING \\n\",\n", |
| 859 | + "]\n", |
| 860 | + "\n", |
| 861 | + "with open('datafiles/bright_modified.txt', 'a') as f: \n", |
| 862 | + " f.writelines(new_lines) " |
| 863 | + ] |
| 864 | + }, |
| 865 | + { |
| 866 | + "cell_type": "code", |
| 867 | + "execution_count": null, |
| 868 | + "metadata": { |
| 869 | + "scrolled": true |
| 870 | + }, |
| 871 | + "outputs": [], |
| 872 | + "source": [ |
| 873 | + "# Print file contents after modifications \n", |
| 874 | + "print('### File content AFTER modifications ###\\n')\n", |
| 875 | + "with open('datafiles/bright_modified.txt') as f: \n", |
| 876 | + " lines = f.readlines() \n", |
| 877 | + "for line in lines:\n", |
| 878 | + " print(line)" |
| 879 | + ] |
| 880 | + }, |
| 881 | + { |
| 882 | + "cell_type": "markdown", |
| 883 | + "metadata": {}, |
| 884 | + "source": [ |
| 885 | + "**IMPORTANT** \n", |
| 886 | + "<br>\n", |
| 887 | + "<br>\n", |
| 888 | + "Python has 6 different file access modes, which control the type of operations you can perform on an opened file ([https://www.geeksforgeeks.org/reading-writing-text-files-python/](https://www.geeksforgeeks.org/reading-writing-text-files-python/)).\n", |
| 889 | + "<br>\n", |
| 890 | + "- **Read Only (‘r’)** : Open text file for reading. The handle is positioned at the beginning of the file. If the file does not exists, raises I/O error. This is also the default mode in which the file is opened.\n", |
| 891 | + "- **Read and Write (‘r+’)** : Open the file for reading and writing. The handle is positioned at the beginning of the file. Raises I/O error if the file does not exist.\n", |
| 892 | + "- Write Only (‘w’) : Open the file for writing. For existing files, the data is truncated and overwritten. The handle is positioned at the beginning of the file. Creates the file if the file does not exist.\n", |
| 893 | + "- **Write and Read (‘w+’)** : Open the file for reading and writing. For existing files, the data is truncated and overwritten. The handle is positioned at the beginning of the file.\n", |
| 894 | + "- **Append Only (‘a’)** : Open the file for writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data.\n", |
| 895 | + "- **Append and Read (‘a+’)** : Open the file for reading and writing. The file is created if it does not exist. The handle is positioned at the end of the file. The data being written will be inserted at the end, after the existing data." |
| 896 | + ] |
730 | 897 | }
|
731 | 898 | ],
|
732 | 899 | "metadata": {
|
|
0 commit comments