we'll sometimes omit in this book to save space)and as we'll seeclose calls are often optionalunless you need to open and read the file again during the same program or sessionc:\temppython file open('data txt'' 'file write('hello file world!\ ' file write('bye file world \ ' file close(open output file objectcreates writes strings verbatim returns number chars/bytes written closed on gc and exit too and that' it--you've just generated brand-new text file on your computerregardless of the computer on which you type this codec:\tempdir data txt / data txt :\temptype data txt hello file worldbye file world there is nothing unusual about the new fileherei use the dos dir and type commands to list and display the new filebut it shows up in file explorer guitoo opening in the open function call shown in the preceding examplethe first argument can optionally specify complete directory path as part of the filename string if we pass just simple filename without paththe file will appear in python' current working directory that isit shows up in the place where the code is run herethe directory :\temp on my machine is implied by the bare filename data txtso this actually creates file at :\temp\data txt more accuratelythe filename is relative to the current working directory if it does not include complete absolute directory path see "current working directoryon page ()for refresher on this topic also note that when opening in modepython either creates the external file if it does not yet exist or erases the file' current contents if it is already present on your machine (so be careful out there--you'll delete whatever was in the file beforewriting notice that we added an explicit \ end-of-line character to lines written to the fileunlike the print built-in functionfile object write methods write exactly what they are passed without adding any extra formatting the string passed to write shows up character for character on the external file in text filesdata written may undergo lineend or unicode translations which we'll describe aheadbut these are undone when the data is later read back output files also sport writelines methodwhich simply writes all of the strings in list one at time without adding any extra formatting for examplehere is write lines equivalent to the two write calls shown earlierfile writelines(['hello file world!\ ''bye file and directory tools file world \ ']