What value does the readline () method return upon encountering end of file quizlet?

Upgrade to remove ads

Only SGD 41.99/year

  • Flashcards

  • Learn

  • Test

  • Match

  • Flashcards

  • Learn

  • Test

  • Match

Terms in this set (23)

open() function

used to get input from a file rather than from a user typing on a keyboard

variable = open ('file.txt')

methods to read text from a file are

read() method returns the file contents as a string. The readlines() method returns a list of strings. file.readline(), returns a single line at a time, which is useful when dealing with large files where the entire file contents may not fit into the available system memory.

variable = open ('file.txt')
contents = variable.readthing()

an optional parameter is to read up to a certain amount of bytes, put the number in as the parameter

Iterating over the lines of a file.

f = open('myfile.txt')

for line in f:
print(line)

f.close()

write to a file to store data permanently.

file.write() method will add only string to a file

Ex:
f = open('myfile.txt', 'w') # Open file
f.write('Example string:\n test...') # Write string
f.close() # Close the file

read mode can be specified in the open()

Read mode 'r' opens a file for reading.
Write mode 'w+' opens a file for writing. and overwrites and reads
Append mode 'a+' opens a file for writing, adds to the end of file and allows reading

Output to a file is buffered by the interpreter
so how do you print

use the \n character

adjust buffering in open()

open(buffering=0) turns off
open(buffering=1) turns on
open(buffering=(any num>1)) turns on until reaches that num in bytes

closing a file, or using file.flush will

force the interpreter to flush the output buffer to disk

he write() method does not immediately writes data to a file, why is this?

output is buffered, thus there may be a delay between calling the write method and data being written to the disk.

to interact with the computer's file system,

a program must use functions supplied by the operating system bc he computer's os controls the file system

files are found differently on differen operating systems, so it is good practice to

utilize the os.path module, which contains many portable functions for handling file paths.

tokens = 'C:\\Users\\BWayne\\tax_return.txt'.split(os.path.sep)
will

separate the parts of the file code at the separator for that system, and
os.path.sep stores the path separator for current operating system.

os.path.join(the tokens list)
will join the tokens with the system's os separators

os.walk() function 'walks' a

directory tree

The os.walk() function is used as the iterable object in a for loop that yields a 3-tuple for each iteration.WALK The first item dirname contains the path to the current directory. The second item subdirs is a list of all the subdirectories of the current directory. The third item files is a list of all the files residing in the current directory.

A programmer might use os.walk() when

when searching for specific files within a directory tree, and the exact path is unknown. Another common task is to filter files based on their file extensions (.pdf, .txt, etc),

A with statement can be used to

open a file, execute a block of statements, and automatically close the file when complete.

se a with statement when opening files, to guarantee that the file is closed when no longer needed.

When using a with statement to open a file, the file is automatically

closed when the statements in the block finish executing.

What does file.readline() do?

ends at \n or an actual new line
use a while loop
until you get an empty string

what does file.read() do?

the entire file is read and made into one single string including the characters \n at new lines

what does file.readlines() do?

the entire file is read into a list and includes the \n characters

how do you get rid of \n or white space in a file that was read ( or other things)

use the strip() function
which will take a string and remove white space at the beginning and end of the string
or use the split() function
which will take a string and split it at the whitespace or at the parameter given

iterate over a file without white space before or after each line

for line in file:
print(line.strip()))

OR

while line != '':
print(line.strip())
line = file.readline()

OR

while True:
line = file.readline()
if line == '':
break
print(line.strip())

line.startswith(
'character')

will see if a line starts with the character imputed and return True or False (?)

open(file, mode) as var
does what

creates a variable file object

Sets found in the same folder

CS 108: Intro to Computing 1.1-1.8, 2.1-2.6 Error…

65 terms

sietsenr

Linux

14 terms

sietsenr

CS 108 2.8, 3.1-3.5 string, list, dict Python

43 terms

sietsenr

CS 108 3.6-3.9, 4.1-4.7 Boolean python

22 terms

sietsenr

Other sets by this creator

Spanish Unit 4 - goodbyes; more emotions; 100-1000…

53 terms

sietsenr

Spanish Unit 3 - days of week + random ASL stuff

36 terms

sietsenr

Spanish Unit 1; numbers + letters

129 terms

sietsenr

Spanish Unit 2; greetings + addressing people

50 terms

sietsenr

Verified questions

computer science

The negation operator is A) Unary B) Binary C) Ternary D) None of the above

Verified answer

computer science

How can you use an IntVar object to determine whether a Checkbutton has been selected?

Verified answer

computer science

Write a program that asks for the name of a pole vaulter and the dates and vault heights (in meters) of the athlete’s three best vaults. It should then report, in order of height (best first), the date on which each vault was made and its height. Input Validation: Only accept values between 2.0 and 5.0 for the heights.

Verified answer

computer science

What output is produced by the following code fragment? String m1, m2, m3; m1="Quest for the Holy Grail"; m2=m1.toLowerCase(); m3=m1+" "+m2; System.out.println (m3.replace('h', 'z'));

Verified answer

Recommended textbook solutions

What value does the readline () method return upon encountering end of file quizlet?

Fundamentals of Database Systems

7th EditionRamez Elmasri, Shamkant B. Navathe

687 solutions

What value does the readline () method return upon encountering end of file quizlet?

Service Management: Operations, Strategy, and Information Technology

7th EditionJames Fitzsimmons, Mona Fitzsimmons

103 solutions

What value does the readline () method return upon encountering end of file quizlet?

Information Technology Project Management: Providing Measurable Organizational Value

5th EditionJack T. Marchewka

346 solutions

What value does the readline () method return upon encountering end of file quizlet?

Starting Out with Python

4th EditionTony Gaddis

629 solutions

Other Quizlet sets

Computer Apps Final Exam

48 terms

Anpzzino44

MIS Final

96 terms

samburns1999

Productivity Software 2019-1

60 terms

Jonnys17

Comp Sci Chapter 4 Theory Test- Sections 4.5 & 4.6

19 terms

sc921

Related questions

QUESTION

A _ is a collection of forms, reports, queries, and programs that serves as an intermediary between users and database data?

2 answers

QUESTION

5. If a nonessential service is slowing down startup, how can you permanently disable it?

10 answers

QUESTION

If application software has stopped responding and the hard disk's LED blinks repeatedly, the operating system probably is doing this.

4 answers

QUESTION

Restricting the order of reads and writes to a particular location is called:

2 answers

What is returned when the readline method reaches the end of the file?

The readline method reads one line from the file and returns it as a string. The string returned by readline will contain the newline character at the end. This method returns the empty string when it reaches the end of the file.

Which method will return an empty string when it has attempted to read beyond the end of?

Note that the readline method returns an empty string ( "" ) when it attempt to read beyond the end of the file.

What is the purpose of closing a file closing a file disconnects the program from the file?

It is important to close files you are working with as soon as you are finished with them. This minimizes the amount of user virtual machine resources required and helps keep shared files and directories available for other users. Closing a file means logically disconnecting it from the application program.

What is the process of retrieving data from a file called?

Data recovery is the process of restoring data that has been lost, accidentally deleted, corrupted or made inaccessible. In enterprise IT, data recovery typically refers to the restoration of data to a desktop, laptop, server or external storage system from a backup.