Navigation

A file is some information or data which stays in the computer storage devices. You already know about different kinds of file , like your music files, video files, text files. Python gives you easy ways to manipulate these files. Generally we divide files in two categories, text file and binary file. Text files are simple text where as the binary files contain binary data which is only readable by computer.

Always make sure you explicitly close each open file, once its job is done and you have no reason to keep it open.
Because
- There is an upper limit to the number of files a program can open. If you exceed that limit, there is no reliable way of recovery, so the program could crash.
- Each open file consumes some main-memory for the data-structures associated with it, like file descriptor/handle or file locks etc. So you could essentially end-up wasting lots of memory if you have more files open that are not useful or usable.
- Open files always stand a chance of corruption and data loss.

Let us try to write an application which will count the spaces, tabs, and lines in any given file.

#!/usr/bin/env python3importosimportsysdefparse_file(path):""" Parses the text file in the given path and returns space, tab & new line details. :arg path: Path of the text file to parse :return: A tuple with count of spacaes, tabs and lines. """fd=open(path)i=0spaces=0tabs=0fori,lineinenumerate(fd):spaces+=line.count(' ')tabs+=line.count('\t')#Now close the open filefd.close()#Return the result as a tuplereturnspaces,tabs,i+1defmain(path):""" Function which prints counts of spaces, tabs and lines in a file. :arg path: Path of the text file to parse :return: True if the file exits or False. """ifos.path.exists(path):spaces,tabs,lines=parse_file(path)print("Spaces %d. tabs %d. lines %d"%(spaces,tabs,lines))returnTrueelse:returnFalseif__name__=='__main__':iflen(sys.argv)>1:main(sys.argv[1])else:sys.exit(-1)sys.exit(0)

You can see that we have two functions in the program , main and parse_file where the second one actually parses the file and returns the result and we print the result in main function. By splitting up the code in smaller units (functions) helps us to organize the codebase and also it will be easier to write test cases for the functions.