留学生考试辅导 File Input/Output

File Input/Output
Sue Inn Chng
The University of 1
Image credit: https://knowledgequest.aasl.org/wp-content/uploads/2021/02/twilight-scaled.jpg

Copyright By PowCoder代写 加微信 powcoder

Input/Output
– Standard input – input()
– Command line arguments
– File inputs – read from file
– Standardoutput–print() – File output – write to file
Your task:
• Go to Ed Lesson. Review the codes in demo.py.
• Write comments for each code block to explain the purpose of each code
The University of 2

Why should I use file input/output?
– Programs that we have written runs for a short time, uses data supplied by users from the terminal and produces transient data.
– Programs you write in the future may: – Runindefinitely.
– Producedatathatyouwillneedtocaptureatdifferentpointsoftimeto be used by others some time in the future.
– Usedatathatisstoredinmultiplefiles.
The University of 3

Read and Write IRL
Read Write
• Locate book.
• Check book details – name, size,
• Locate book.
• Check book details – name, size,
• Open Book.
• Read [all/specified] contents from
• Open Book.
• Write [all/specified] contents to
Close Book 0
Your task:
colour/author?
• Permission to access book.
• Permission to access book.
[start/page] to [end/page] Close Book
[start/location/end] of page
• Did you notice anything in common between both read and write tasks?
• Highlight the common steps.
The University of 4

General Process of File I/O
1. Create file object with the correct mode (access permission).
– open(file, mode)
– https://docs.python.org/3/library/functions.html#open
2. Callfileobjectmethodstoreadorwritetofile. – read(size)
– readline()
– readlines()
– write(text)
– https://docs.python.org/3/library/io.html#module-io
3. Close file using file object method close(). The University of 5

File Input: read(),readline(),readlines()
– Canyouidentifythelinenumbersthat: • Create a file object in read mode?
• Read the first line of the file?
• Close the file object?
– GotoEdLesson.Runtheprogramand observe the output.
– Determine the output of the program if you modify Line 3 to:
• read_contents = fobj.read()?
• read_contents = fobj.readlines()?
The University of 6

File Output: write(), print() – Go to Ed Lesson. Review the codes in demo.py.
– Can you identify the line numbers that: • Create a file object in write mode?
• Write a given string to the file?
• Close the file object?
– Run the program. Open the file and check the output in the terminal. Explain your observations.
The University of 7

Summary: Input/Output
– Standard input – input()
– Standard output – print()
– Command line arguments
– File output – write to text file
– File inputs – read from text file
• write() -> int
The University of 8
• read()-> str
• writelines() -> None
• readline() -> str
• readlines() -> list

Exercise: Shopping List
– ReadthequestionandextractinformationabouttheIPOofthe program.
– Process – Outputs
The University of 9

Reading This Week
– Chapter9and14.Downey,A.B.(2015).ThinkPython:Howto Think Like a Computer Scientist (2e ed.). O’ , Incorporated.
– Chapter1.5.RedirectionandPiping.Sedgewick,R.,Wayne, K., & Dondero, R. (2015). Introduction to programming in Python: An interdisciplinary approach. Addison- .
The University of 10

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com