![]() If you are a Mac user, omit the drive letter " C:" from your file path.For more information on the len() function, see Tutorial 11. To recall the length in words of your text file, type len(booktxt).For example, booktxt will recall the first line of the text from your file, which in the text we are using is formatting information. In this case, you can also recall select lines from your text by placing the specific line number within brackets next to your variable. While this feature can be useful in other circumstances, oftentimes your text will be too long and unwieldy to be recalled in this manner, as is the case with the large text file used in this tutorial. After defining this variable, simply typing booktxt will display your entire text file start to finish within your IDLE window.For example, typing booktxt = book.readlines() will define "booktxt" as your text file and allow it to be recalled within your program on a readable line by line basis. After opening your text file, you can tell Python what to do with it by defining it is a variable. Within the open() function, type a string containing the path of the location of your text file (in this case, it looks like open('C:/Users/mybringback/Desktop/pg16328.txt'), your location will of course look different). To open a text file within your code, use Python's built in open() function.Interactive IDLE session from this tutorial.On this page: reading from a file, open().
0 Comments
Leave a Reply. |
AuthorWrite something about yourself. No need to be fancy, just an overview. ArchivesCategories |