Working with files in Python
Tags: python, beginners, tutorial
Published: 2020-06-13 14:59:14

I am sure we all have worked with files in some shape or form. Almost all programming languages provide file management capability. In this article, let’s see how to manipulate files in Python.

<h4>How to read files?</h4>
In Python, files could be read and written using the built-in `open` function. 
```python
def main():
    fobj = open("quotes.txt", mode="rt")
    for line in fobj:
        print(line.rstrip())
    fobj.close()
  

if __name__ == "__main__":
    main()
```
* Open function creates a file handle which could be used to iterate over the content of the file each line at a time.
* We have explicitly indicated that the file should be open for reading in text mode using the `rt` argument. This is the default mode.
* Binary files like images can be manipulated using `rb` mode.
* If the file being read is not present, execution of the program will result in a file or directory not found error.
* Above listing is using String’s `rstrip()` function to remove trailing whitespaces during printing.

<h4>How to write to files?</h4>
Files can be opened for writing by opening a file in the `write` mode like shown below:
```python
def main():
    quotes_list = [
        "Always remember that you are absolutely unique. Just like everyone else. -Margaret Mead",
        "When you reach the end of your rope, tie a knot in it and hang on.  -Franklin D. Roosevelt",
        "Don't judge each day by the harvest you reap but by the seeds that you plant. -Robert Louis Stevenson",
        "The future belongs to those who believe in the beauty of their dreams. -Eleanor Roosevelt",
        "It is during our darkest moments that we must focus to see the light. -Aristotle",
        "Do not go where the path may lead, go instead where there is no path and leave a trail. -Ralph Waldo Emerson"
    ]
    fobj = open("quotes.txt", "wt")
    for quote in quotes_list:
        fobj.write(quote + "\n")
    fobj.close()    


if __name__ == "__main__":
    main()
```
Files opened using `wt` mode will be overwritten. Opening file with `mode="at"` appends new contents to the end of the file. `mode="r+t"` opens the file for reading and updating in text mode.
 
<h4>Copying files using With option</h4>  
Resources like files need to be closed when open for reading or writing. File’s data could end up in an inconsistent state if we forget to close the file object at the end of use especially while writing to a file. To avoid this problem, we can open files using `with` context-manager:
```python
def main():
    with open("quotes.txt", mode="rt") as robj, open("quotes_copy.txt", mode="wt") as wobj:
        for line in robj:
            print(line.rstrip(), file=wobj)


if __name__ == "__main__":
    main()
```

<h4>Reading all of the contents at once</h4>
All of the contents of a file can be read at once using the `readlines` method of a file object.
```python
with open("quotes.txt") as fobj:
    print(fobj.readlines())
```
* Above `readlines` method generates a list containing individual lines as its member.
* It is recommended to only use the above method for smaller files.

<h4>Working with binary files</h4>
In all of the above examples we worked with text files and opened files in text mode. But files like images have to be manipulated using binary mode. Let’s see how to manipulate a file in binary mode by copying an image:
```python
def copy():
    with open("cat-with-kitten.jpg", mode="rb") as rfile, open("cat-with-kitten-copy.jpg", mode="wb") as wfile:
        while True:
            buf = rfile.read(10240)
            if buf:
                wfile.write(buf)
            else:
                break;    
    print("Done with copying file")


if __name__ == "__main__":
    copy()
```
* To copy a binary file, a slightly different approach is used as sometimes binary files could be quite large in size. 
* The program is reading a small chunk of file at a time and copying to the output file. 
* Above program has a `while` loop that is initially set to continue forever. And the program terminates the loop when there are no more chunks left to copy.