Working with files in Python

dev0928

dev0928

Posted on June 13, 2020

Working with files in Python

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.

How to read files?

In Python, files could be read and written using the built-in open function.

def main():
    fobj = open("quotes.txt", mode="rt")
    for line in fobj:
        print(line.rstrip())
    fobj.close()


if __name__ == "__main__":
    main()
Enter fullscreen mode Exit fullscreen mode
  • 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.

How to write to files?

Files can be opened for writing by opening a file in the write mode like shown below:

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()
Enter fullscreen mode Exit fullscreen mode

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.

Copying files using With option

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:

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()
Enter fullscreen mode Exit fullscreen mode

Reading all of the contents at once

All of the contents of a file can be read at once using the readlines method of a file object.

with open("quotes.txt") as fobj:
    print(fobj.readlines())
Enter fullscreen mode Exit fullscreen mode
  • Above readlines method generates a list containing individual lines as its member.
  • It is recommended to only use the above method for smaller files.

Working with binary files

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:

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()
Enter fullscreen mode Exit fullscreen mode
  • 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.
💖 💪 🙅 🚩
dev0928
dev0928

Posted on June 13, 2020

Join Our Newsletter. No Spam, Only the good stuff.

Sign up to receive the latest update from our blog.

Related