operations |
comment | "parent_author":"",<br>"parent_permlink":"utopian-io",<br>"author":"scipio",<br>"permlink":"learn-python-series-12-handling-files",<br>"title":"Learn Python Series (#12) - Handling Files",<br>"body":"# Learn Python Series (#12) - Handling Files\n\n![python_logo.png (https:\/\/res.cloudinary.com\/hpiynhbhq\/image\/upload\/v1521591626\/vsbvkflohhnwyfb6wwdf.png)\n\n#### What Will I Learn?\nYou will learn:\n- Some basic file handling operations,<br>\n- such as how to get your current working directory using the `os` module,<br>\n- how to set a file path,<br>\n- how to `open()` files from your file path,<br>\n- how to use the `open()` function to read data from,<br> and write data to your files,<br>\n- by using various `mode` flags,<br>\n- with which you can both read and write,<br> via various techniques,<br> your file data,<br>\n- and finally how to use the `with` keyword,<br> using an alternative syntax without the need to explicitly `close()` your previously opened files.\n\n#### Requirements\n- A working modern computer running macOS,<br> Windows or Ubuntu\n- An installed Python 3(.6) distribution,<br> such as (for example) the Anaconda Distribution\n- The ambition to learn Python programming\n\n#### Difficulty\nBasic\n\n\n\n#### Curriculum (of the `Learn Python Series`):\n- [Learn Python Series - Intro (https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-intro)\n- [Learn Python Series (#2) - Handling Strings Part 1 (https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-2-handling-strings-part-1)\n- [Learn Python Series (#3) - Handling Strings Part 2 (https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-3-handling-strings-part-2)\n- [Learn Python Series (#4) - Round-Up #1 (https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-4-round-up-1)\n- [Learn Python Series (#5) - Handling Lists Part 1 (https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-5-handling-lists-part-1)\n- [Learn Python Series (#6) - Handling Lists Part 2 (https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-6-handling-lists-part-2)\n- [Learn Python Series (#7) - Handling Dictionaries (https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-7-handling-dictionaries)\n- [Learn Python Series (#8) - Handling Tuples (https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-8-handling-tuples)\n- [Learn Python Series (#9) - Using Import (https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-9-using-import)\n- [Learn Python Series (#10) - Matplotlib Part 1 (https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-10-matplotlib-part-1)\n- [Learn Python Series (#11) - NumPy Part 1 (https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-11-numpy-part-1)\n\n# Learn Python Series (#12) - Handling Files\nAs promised in the previous episode of the `Learn Python Series` in this episode we will learn how to handle files: opening files,<br> reading from existing files,<br> creating new files,<br> and writing to files.\n\n# Reading the contents of a `.txt` file\nLet's begin with creating a simple text file,<br> using your favorite code editor,<br> not Python for now,<br> save it as `example.txt` in our current working directory including the following content:\n\n```\nMonths of the year:\nJanuary\nFebruary\nMarch\nApril\nMay\nJune\nJuly\nAugust\nSeptember\nOctober\nNovember\nDecember\n```\n\n### The `open()` function\n\nUsage: ` open(file,<br> mode='r',<br> buffering=-1,<br> encoding=None,<br> errors=None,<br> newline=None,<br> closefd=True,<br> opener=None)`\n\nIn Python,<br> in order to open a stored file,<br> you first need to associate that file with a variable and to show Python where that file is located on disk,<br> called the `file path`. Since you might be following along on Windows,<br> Linux,<br> macOS,<br> your file paths are always different. So to make sure everybody,<br> regardless of which type of computer system you are working on right now,<br> can follow along,<br> let's first import the standard library module `os` and assign the current working directory to the variable `current_dir` and concatenate the file name `example.txt` to it,<br> in order to make sure the new variable `file_path` contains the correct path string,<br> like so:\n\n\n```python\nimport os\n\ncurrent_dir = os.getcwd()\nfile_path = current_dir + '\/example.txt'\n```\n\nNow we will associate the file on disk to the newly created variable `f` (short for: file) using the `open()` function. The `open()` function will return a file object. `open()` allows for receiving many optional arguments,<br> but as its first argument it **requires** the file path.\n\nA very important second parameter for `open()` is the `mode parameter`,<br> which is set by default to `r` (for reading a file).\n\nCommonly used mode option arguments are:\n- `'r'`: open file for reading (default)\n- `'w'`: open file for writing,<br> but truncating the file (overwrite the current contents)\n- `'a'`: open file for writing data to the end of its contents\n- `'x'`: create a new file and write data to it\n- `'r+'`: open file for first reading and then writing to the same file.\n\n\n```python\nf = open(file_path,<br> 'r')\n```\n\n### File reading with `read()`,<br> `readline()` and \/ or `readlines()`\nNow that we have associated the file `example.txt` stored on disk in our current working directory by opening it,<br> the variable `f` contains the file object. There are three built-in Python file methods that read data from a file:\n\n- `read()`: returns all file contents\n- `readline()`: reads \"the next line\",<br> and steps through the file contents when calling it multiple times\n- `readlines()`: reads all file lines and returns them as a list\n\n**Nota bene:** if you first use `read()` and then again try to use `readline()` or `readlines()`,<br> an empty result will be returned. If you must use multiple read methods then first open the file again and assign it to a new variable (e.g. `f_new`).\n\n### `read()`\nThis would be an example of how to use `read()`:\n\n\n```python\ncontent = f.read()\nprint(content)\n```\n\n Months of the year:\n January\n February\n March\n April\n May\n June\n July\n August\n September\n October\n November\n December\n \n\n\n### `readline()`\nThis would be an example of how to use `readlines()` (once,<br> or multiple times):\n\n\n```python\nfirst_line = f.readline()\nprint(first_line)\n\nsecond_line = f.readline()\nprint(second_line)\n```\n\n Months of the year:\n \n January\n \n\n\n### `readlines()`\nAnd we could have returned all individual content lines in the file `example.txt` as a list,<br> like so:\n\n\n```python\nlines = f.readlines()\nprint(lines)\n```\n\n ['Months of the year:\\n',<br> 'January\\n',<br> 'February\\n',<br> 'March\\n',<br> 'April\\n',<br> 'May\\n',<br> 'June\\n',<br> 'July\\n',<br> 'August\\n',<br> 'September\\n',<br> 'October\\n',<br> 'November\\n',<br> 'December\\n' \n\n\n# Writing content to a new `.txt` file\nLet's say we want to store the string value of the variable `abba` to a newly created file called `abba.txt` and save that to our current working directory.\n\nLet's first import the `os` module again,<br> get the `current_dir` and concatenate our file name `abba.txt` to it as `file_path` so we know what to store where on disk.\n\n\n```python\nimport os\n\ncurrent_dir = os.getcwd()\nfile_name = 'abba.txt'\nfile_path = current_dir + '\/' + file_name\n\nabba = '''So I say\nThank you for the music,<br> the songs I'm singing\nThanks for all the joy they're bringing\nWho can live without it,<br> I ask in all honesty\nWhat would life be?\nWithout a song or a dance what are we?\nSo I say thank you for the music\nFor giving it to me\n'''\n```\n\nWe can now simply use the `open()` function again,<br> this time - since we want to create a new file and write content to it,<br> using the `mode=x` argument,<br> like so:\n\n\n```python\nf = open(file_path,<br> mode='x')\n```\n\nAfter executing this line,<br> the file `abba.txt` is indeed created on disk,<br> but of course no content has been written to it yet.\n\n### Using the `write()` method\nTo do that,<br> we use the `write()` method and pass in the string `abba` (that contains the data,<br> the song lyrics) as its argument,<br> like so:\n\n\n```python\nf.write(abba)\n```\n\n\n\n\n 254\n\n\n\n### Closing the file with `close()`\nNow the data has been written to the file,<br> but in order for other programs to access (e.g. read from) the file,<br> we first need to close the Python file object,<br> thereby terminating the connection between the file object and the file stored on disk. Like so:\n\n\n```python\nf.close()\n```\n\nDid it work? Use your file browser,<br> check if the file `abba.txt` is indeed located in the current working directory,<br> and open it using your favorite editor. Is it there? Are the song lyrics stored inside it? **Great!**\n\n# Appending content to an existing `.txt` file\nUsing the `open()` function again,<br> this time using the `mode=a` flag,<br> we can **append** new content to the end of an existing file. The `a` flag first opens the file,<br> doesn't actually read its content,<br> but using the `write()` method where (again) a string of data is passed as an argument,<br> it appends (= writes to the end of the file) that string of data. Like so:\n\n\n```python\nimport os\n\ncurrent_dir = os.getcwd()\nfile_path = current_dir + '\/abba.txt'\n\nsome_more_text = '----------\\nThis is a famous song by ABBA.\\n'\n\nf = open(file_path,<br> 'a')\nf.write(some_more_text)\nf.close()\n```\n\nNow if you open the file `abba.txt` in your editor again,<br> if all went well,<br> you should see the contents of the variable `some_more_text` is indeed added at the end of what was in the file before:\n\n```\nSo I say\nThank you for the music,<br> the songs I'm singing\nThanks for all the joy they're bringing\nWho can live without it,<br> I ask in all honesty\nWhat would life be?\nWithout a song or a dance what are we?\nSo I say thank you for the music\nFor giving it to me\n----------\nThis is a famous song by ABBA.\n```\n\n# Appending to or overwriting parts of an existing `.txt` file\nUsing `open()` with the `r+` flag enables you to both read the existing file contents,<br> do something with those contents (using for example the built-in string methods we discussed earlier),<br> and also (over)write (some parts of) that same file.\n\nLet's see the behavior of the `r+` flag by overwriting (parts of) the first file line with some dummy data,<br> like so:\n\n\n```python\nimport os\ncurrent_dir = os.getcwd()\nfile_path = current_dir + '\/abba.txt'\ndummy_data = 'BlaBlaBlaBlaBla'\n\nf = open(file_path,<br> 'r+')\nf.write(dummy_data)\nf.close()\n```\n\nIf you open your `abba.txt` file again,<br> its contents are like this:\n```\nBlaBlaBlaBlaBlayou for the music,<br> the songs I'm singing\nThanks for all the joy they're bringing\nWho can live without it,<br> I ask in all honesty\nWhat would life be?\nWithout a song or a dance what are we?\nSo I say thank you for the music\nFor giving it to me\n----------\nThis is a famous song by ABBA.\n```\n\nDid you notice that the first line now contains the `BlaBlaBlaBlaBla` string,<br> and that the rest of the original file contents is still there? Ok...\n\nNow we could also use the `r+` flag to append the a string to the end of the file. In order to do that,<br> we must first read the existing file contents (so the file pointer is at the end),<br> and then write the appending string to the file. \n\nFor example:\n\n\n```python\nimport os\ncurrent_dir = os.getcwd()\nfile_path = current_dir + '\/abba.txt'\nf = open(file_path,<br> 'r+')\n\nappending_string = '\\n---------\\nAlso a way to append data!\\n'\noriginal = f.read()\n\nf.write(appending_string)\nf.close()\n```\n\nThe result file is now as this:\n```\nBlaBlaBlaBlaBlayou for the music,<br> the songs I'm singing\nThanks for all the joy they're bringing\nWho can live without it,<br> I ask in all honesty\nWhat would life be?\nWithout a song or a dance what are we?\nSo I say thank you for the music\nFor giving it to me\n----------\nThis is a famous song by ABBA.\n\n---------\nAlso a way to append data!\n```\n\n# Truncating and overwriting an existing `.txt` file\nUsing the `open()` function wiht the `w` flag first truncates the content of the existing file,<br> and then writes the new content passed into the `write()` function.\n\n\n```python\nimport os\n\ncurrent_dir = os.getcwd()\nfile_path = current_dir + '\/abba.txt'\n\nnew_content = \"This is completely new content.\\n\"\n\nf = open(file_path,<br> 'w')\nf.write(new_content)\nf.close()\n```\n\nThe ABBA song lyrics are now deleted from the file `abba.txt` and replaced with the string `This is completely new content.\\n`. Like so:\n\n```\nThis is completely new content.\n```\n\n# Using `with open() as f:`\nWhen working with file objects,<br> it's oftentimes convenient to use the `with` keyword. Its syntax is a bit different than what we've been discussing until now,<br> but the advantage of using `with` is that the file you're working on is automatically closed when you're done with your file procedure.\n\nPlease regard the following example:\n\n\n```python\nimport os\n\ncurrent_dir = os.getcwd()\nfile_path = current_dir + '\/with.txt'\n\ncontent = '''In west Philadelphia born and raised\nOn the playground where I spent most of my days\nChilling out,<br> maxing,<br> relaxing all cool\nAnd all shooting some b-ball outside of the school\nWhen a couple of guys,<br> they were up to no good\nStarted making trouble in my neighbourhood\nI got in one little fight and my mom got scared\nAnd said You're moving with your auntie and uncle in Bel-air\n'''\n\nwith open(file_path,<br> 'w') as f:\n f.write(content)\n```\n\nIf you open the file `with.txt` as expected,<br> its contents will be:\n```\nIn west Philadelphia born and raised\nOn the playground where I spent most of my days\nChilling out,<br> maxing,<br> relaxing all cool\nAnd all shooting some b-ball outside of the school\nWhen a couple of guys,<br> they were up to no good\nStarted making trouble in my neighbourhood\nI got in one little fight and my mom got scared\nAnd said You're moving with your auntie and uncle in Bel-air\n```\n\n# What did we learn,<br> hopefully?\n\nWe've discussed how to get,<br> using the `os` module,<br> your current working directory,<br> `open()` files in it,<br> read data from,<br> and write data to your files. We've discussed various `mode` flag options of the `open()` function,<br> how you can use them for various purposes,<br> and we've discussed using the `with` keyword as an alternative mechanism that doesn't need explicit file closing.\n\nOf course,<br> since this was the first time we read content from and written some other content to a file,<br> using Python,<br> I've deliberately kept the explanations and the examples pretty simple to understand.\n\nBut in the next tutorial episodes,<br> we will use the knowledge egarding handling files we learned today and build upon that to use and manipulate some more complex data files.\n\n### Thank you for your time!\n \n\n<br \/><hr\/><em>Posted on <a href=\"https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-12-handling-files\">Utopian.io - Rewarding Open Source Contributors<\/a><\/em><hr\/>",<br>"json_metadata":" \"community\":\"utopian\",<br>\"app\":\"utopian\/1.0.0\",<br>\"format\":\"markdown\",<br>\"repository\"<br>\"id\":81598961,<br>\"name\":\"cpython\",<br>\"full_name\":\"python\/cpython\",<br>\"html_url\":\"https:\/\/github.com\/python\/cpython\",<br>\"fork\":false,<br>\"owner\"<br>\"login\":\"python\" ,<br>\"pullRequests\":[ ,<br>\"platform\":\"github\",<br>\"type\":\"tutorials\",<br>\"tags\":[\"utopian-io\",<br>\"steemdev\",<br>\"steemstem\",<br>\"open-source\",<br>\"python\" ,<br>\"users\":[\"scipio\" ,<br>\"links\":[\"https:\/\/res.cloudinary.com\/hpiynhbhq\/image\/upload\/v1521591626\/vsbvkflohhnwyfb6wwdf.png\",<br>\"https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-intro\",<br>\"https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-2-handling-strings-part-1\",<br>\"https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-3-handling-strings-part-2\",<br>\"https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-4-round-up-1\",<br>\"https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-5-handling-lists-part-1\",<br>\"https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-6-handling-lists-part-2\",<br>\"https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-7-handling-dictionaries\",<br>\"https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-8-handling-tuples\",<br>\"https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-9-using-import\",<br>\"https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-10-matplotlib-part-1\",<br>\"https:\/\/utopian.io\/utopian-io\/@scipio\/learn-python-series-11-numpy-part-1\" ,<br>\"image\":[\"https:\/\/res.cloudinary.com\/hpiynhbhq\/image\/upload\/v1521591626\/vsbvkflohhnwyfb6wwdf.png\" " | comment_options | "author":"scipio", "permlink":"learn-python-series-12-handling-files", "max_accepted_payout":"1000000.000 SBD", "percent_steem_dollars":10000, "allow_votes":true, "allow_curation_rewards":true, "extensions":[[0, "beneficiaries":[ "account":"utopian.pay", "weight":2500 |
|