0% found this document useful (0 votes)
46 views2 pages

Python Week 6 Notes

The document discusses different approaches for reading and writing files in Python including using readline() to read files line by line, reading the entire file content into a list using readlines(), and using a for loop to process every line. It also mentions opening files using open(), writing to files using write(), and GUI modules like tkinter and tkinter.filedialog.

Uploaded by

nathana_3
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
46 views2 pages

Python Week 6 Notes

The document discusses different approaches for reading and writing files in Python including using readline() to read files line by line, reading the entire file content into a list using readlines(), and using a for loop to process every line. It also mentions opening files using open(), writing to files using write(), and GUI modules like tkinter and tkinter.filedialog.

Uploaded by

nathana_3
Copyright
© Attribution Non-Commercial (BY-NC)
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as RTF, PDF, TXT or read online on Scribd
You are on page 1/ 2

for loop over indices

Parellel Lists and Strings Nested Loops - In general, the number of iterations of a nested loop over lists will be

the product of the lengths of the lists.

Reading Files

to open file open(filename, mode) mode 'r' - to open for reading 'w' - to open for writing (erasing what is already in the file) 'a' - to open for appending (adding to what is already in the file) Method readline() readline() read and return the next line from the file, including the newline character (if it exists) return the empty string if there are no more lines in the file. The readline approach 1. use readline to s ip the beginning of the file !. read the first line of the file you are interested in ". while not at the end of the interesting section# process the current line read the next line of the interesting section WHen to use this approach $hen you want to process only some parts of a file. The for line in file approach for line in file# process line When to use this approach $hen you want to process every line in a file

The read approach file.read() When to use this approach - $hen you want to read the whole file at once. The readlines approach list % file.readlines() When to use this approach - $hen you want to be able to examine each line by index. Method write(str) - $rite a string to a file. (unli e function print, this does not append an extra newline character.) Module tkinter &ontains types and functions for creating graphical user interfaces Module tkinter.filedialog - &ontains functions for creating dialogs Developing a rogram

You might also like