In this article you’re going to learn how to work with files in python, you gonna learn various techniques on how open, , , and files in python. read manipulate save Once you finish this article you will be in a good position of in python so I suggest you read this to the end. handling files Getting started Working with files in python is straightforward, you can files and it in just line of code. open read one For instance, let’s try reading from a file which contains quotes as shown below; quotes.txt True humility thinking less yourself; thinking yourself less. I believe Christianity I believe sun has risen: only because I see , because I see everything . You are never too old another goal dream a new dream. is not of it is of in as that the not it but by it else to set or to Reading from file data = open( ).read() print(data) humility thinking less of yourself; it thinking of yourself less. I believe Christianity I believe that the sun has risen: only because I see it, but because by it I see everything . You are never too old to set another goal to dream a new dream. >>> 'quotes.txt' >>> True is not is in as not else or We used the function to a file in python in just one line of code. open () read Diving into the basics Modes of opening files in Python You can open a file in python in different modes such as , , , , and etc. reading mode writing mode appending mode binary mode , python the files in thus why in the example above we didn’t specify the mode but it doesn’t throw any error. By default opens reading mode We use the following abbreviation to specify the mode; r -> for opening file in reading mode w ->for opening file in writing mode a -> for opening file in appending mode r+ ->for opening file in reading and writing mode w+ ->for opening file in writing and reading mode rb -> for reading in binary mode wb -> for writing in binary mode Reading files in python (reading mode) You should use this mode when you're opening a file with the intention to read its content only since once you open a file in reading mode, it restricts you from making any changes to it. To open a file in reading mode you just specify the path to the file plus the reading mode which just as shown below; r file = open( , ) data = file.read() print(data) 'quotes.txt' 'r' When you run the above code it will print the same result as the one we have seen above. When opening a file you use the builtin function it creates a with different methods that you can use to the file which can be viewed by the . open () file object manipulate dir() dir(file) ................. , , , , , , , , , , , , , , , , , , , , , , , >>> 'buffer' 'close' 'closed' 'detach' 'encoding' 'errors' 'fileno' 'flush' 'isatty' 'line_buffering' 'mode' 'name' 'newlines' 'read' 'readable' 'readline' 'readlines' 'seek' 'seekable' 'tell' 'truncate' 'writable' 'write' 'writelines' There is a way to read the and also , for instance, let’s try reading line by line and after each line, we print a line of stars. entire file line by line Reading by line by line file = open( , ) line file: print(line) print( ) humility thinking less of yourself; it thinking of yourself less. *********************************** I believe Christianity I believe that the sun has risen: only because I see it, but because by it I see everything . *********************************** You are never too old to set another goal to dream a new dream. *********************************** >>> 'quotes.txt' 'r' >>> for in ... ... '***********************************' ... True is not is in as not else or Reading a specific line you can also a specific line you need, for instance, Let’s try reading the only seconds from our quotes.txt Note : Index starts with 0, from the bottom of the file to the top of the file file = open( , ) second_line = file.readlines( )[ ] print(second_line) I believe Christianity I believe that the sun has risen only because I see it, but because by it I see everything . >>> 'quotes.txt' 'r' >>> 1 0 >>> in as not else Note : When you open a file in Python you need to close it once you finish manipulating with it because not closing it may make it inaccessible to other programs. Therefore to prevent that you’re advised to open a file using a , which will automatically close the file when the block code finished running. with statement Example of Usage >>> open( , ) file: ... line = file.readline() ... print(line) ... True humility is not thinking less yourself; it is thinking yourself less. with 'quotes.txt' 'r' as of of Working with writing mode To open a file in writing mode you need to use the abbreviation of in reading mode. w We normally use mode when a new file to write information into because if you use this mode to open an existing file, it will replace the existing file content with the one you just wrote For instance Let’s open a new file called numbers and then write a few numbers into: Example of Usage : >>> open( , ) file: ... for number range( ): ... file.write(str(number)) ... with 'numbers.txt' 'w' as in 10 If you open your you find is written to it. number.txt 0123456789 Working with appending mode is used when we want to append new information to an existing file, to open a file in appending mode use on the file mode. Appending mode a For instance, Let’s add a new quote to our quotes.txt Example of Usage >>> open( , ) file: ... new_quote = ... file.write(new_quote) ... ----------reading the file again--------------------- >>> lastline = open( ).readlines()[ ] >>> print(lastline) The world is a tragedy to tho who feel with 'quotes.txt' 'a' as '\nThe world is tragedy to tho who feel' 36 'quotes.txt' -1 As you can in appending mode the quote we just added just saved to our file revealed when we try to open it again. I recommend also reading this; The basics of lambda function in Python Learn how to interact with OS using Python A quick guide to args and kwargs in Python How to speed up python code with memorization How to do multi-threading programming in Python An introductory guide to list comprehension in Python The can be found on original article kalebujordan.com In case of any suggestion or comment, drop it in the comment box and I will get back to you ASAP.