sequencesstringslistsand files one way to loop through the entire contents of file is to read in all of the file using readlines and then loop through the resulting list infile open(somefile" "for line in infile readlines()process the line here infile close(of coursea potential drawback of this approach is the fact that the file may be very largeand reading it into list all at once may take up too much ram fortunatelythere is simple alternative python treats the file itself as sequence of lines so looping through the lines of file can be done directly like thisinfile open(somefile" "for line in infileprocess the line here infile close(this is particularly handy way to process the lines of file one at time opening file for writing prepares that file to receive data if no file with the given name existsa new file will be created word of warningif file with the given name does existpython will delete it and create newempty file when writing to filemake sure you do not clobber any files you will need laterhere is an example of opening file for outputoutfile open("mydata out"" "the easiest way to write information into text file is to use the print statement with which you are already familiar to print to filewe just need to add an extra keyword parameter that specifies the fileprintfile=this behaves exactly like normal print except that the result is sent to outputfile instead of being displayed on the screen example programbatch usernames to see how all these pieces fit togetherlet' redo the username generation program our previous version created usernames interactively by having the user type in his or her name if we were setting up accounts for large number of usersthe process would probably not be done interactivelybut in batch mode in batch processingprogram input and output is done through files our new program is designed to process file of names each line of the input file will contain the first and last names of new user separated by one or more spaces the program produces an output file containing line for each generated username