8 files n.
Skip this Video
Loading SlideShow in 5 Seconds..
8. Files PowerPoint Presentation

8. Files

4 Views Download Presentation
Download Presentation

8. Files

- - - - - - - - - - - - - - - - - - - - - - - - - - - E N D - - - - - - - - - - - - - - - - - - - - - - - - - - -
Presentation Transcript

  1. 8. Files Rocky K. C. Chang 30 October 2018 (Based on Zelle and Dierbach)

  2. Objectives • To understand basic file processing concepts and techniques for reading and writing text files in Python. • To be able to understand and write programs that process textual information.

  3. File (object) • A file is a sequence of data that is stored in secondary memory (disk drive). • Two types of files: text file and binary file • A text file contains characters, structured as lines of text. • A binary file is a file formatted in a way that only a computer program can read. • A text file usually contains more than one line of text. Lines of text are separated with a special character, the newline character.

  4. Exercise 8.1 Open notepad++ (you may need to install it yourself in J drive) Find a .py file and use notepad++ to open it. Under “View/Show Symbols”, choose “Show All Characters” to see the newline and tab characters.

  5. How to end a line? ( • LF: Multics, Unix and Unix-like systems (GNU/Linux, OS X, FreeBSD, AIX, Xenix, etc.), BeOS, Amiga, RISC OS and others. • CR: Commodore 8-bit machines, Acorn BBC, ZX Spectrum, TRS-80, Apple II family, Mac OS up to version 9 and OS-9 • CR+LF: Microsoft Windows, DECTOPS-10, RT-11 and most other early non-Unix and non-IBM OSes, CP/M, MP/M, DOS (MS-DOS, PC DOS, etc.), Atari TOS, OS/2, Symbian OS, Palm OS, Amstrad CPC

  6. Exercise 8.2 Install and open binaryviewer in J drive. Open the same .py file used in the last exercise. Compare the binary representation on the left and the text on the right.

  7. File Processing • Open a file in a secondary storage. • Read / write the file. • Save the file if write. • Close the file.

  8. Opening a file in a secondary storage • If successful, a “file handler” will be returned. • infile = open("mbox.txt", "r") • infile = open("mbox.txt", "w") Source:

  9. Reading/writing and saving a file • Load (part of) the file into the main memory. • Read the file from the memory. • Write the file to the memory. • Saving will write the file in the memory to a secondary storage.

  10. Exercise 8.3 Try infile = open("a_text_file_of_your_choice","r") data = print(data) (You may need os.getcwd() and os.chdir() to find your files.)

  11. Exercise 8.4 Try infile = open("a_text_file_of_your_choice","r") for i in range(10): line = infile.readline() print(line[:-1]) How do you print the lines in a single line?

  12. Exercise 8.5 Try infile = open("a_text_file_of_your_choice","r") for line in infile.readlines(): print(line) Does the output look the same as the file’s?

  13. Three file read methods • <filevar>.read() – returns the entire remaining contents of the file as a single (possibly large, multi-line) string. • <filevar>.readline() – returns the next line of the file. This is all text up to and including the next newline character. • <filevar>.readlines() – returns a list of the remaining lines in the file. Each list item is a single line including the newline characters.

  14. Exercise 8.6 Try input_file = open("an_existing_py_file", "r") output_file = open("", "w") content = output_file.write(content) output_file.close() Find the new file.

  15. Exercise 8.7 Try input_file = open("an_existing_py_file", "r") output_file = open("another_existing_py_file", "a") content = output_file.write(content) output_file.close() Open the output file.

  16. Write and append methods • Opening a file for writing prepares the file to receive data. • If you open an existing file for writing, you wipe out the file’s contents. If the named file does not exist, a new one is created. • It is important to close a file that is written to, otherwise the tail end of the file may not be written to the file.

  17. Exercise 8.8 Download from Prepare an input file and use to generate batch usernames.

  18. Exercise 8.9 Modify by replacing print(uname, file=outfile) with write(). Do you observe any difference in the output file? How do you slightly modify the codes to generate the same outputs?

  19. Exercise 8.10 Please find it on the next few slides.

  20. Write a Python program that has two parts. The first is to ask users to input a starting number and an ending number, and the number of numbers (say col) printed on each row and the number of spaces for printing each number. The program will then write the numbers to a file, and the numbers are separated by "," except for the last number on a line. After writing col numbers, a new line will be started. A sample of the file:

  21. In the second part, the program opens the file and prints them to the screen using the number of spaces specified by the user. Each row is filled up from left to right, and a new row will be used only after a row is filled up. Each number is printed on right-justified format. Three sample outputs:

  22. End