01. Perform Input and Output Operations 1 Flashcards

1
Q

How many arguments does the open function in python use?

A

Two arguments.

  • the path to the file
  • The mode, i.e. read, write, append, binary

workFile = open(‘Jacket’, ‘r’)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
2
Q

In the following code example, the file has been opened, read and the contents displayed onscreen. The user is finished with the file.

What is the next line of code?

workFile = open(‘Jacket’, ‘r’)
workFileContents = workFile.read()
print(workFileContents)

A

workFile = open(‘Jacket’, ‘r’)
workFileContents = workFile.read()
print(workFileContents)
workFile.close()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
3
Q

The following code example reads the entire contents of a file.

What changes must be made to read the first line only.

workFile = open(‘Jacket’, ‘r’)
workFileContents = workFile.read()
print(workFileContents)
workFile.close()

A

workFile = open(‘Jacket’, ‘r’)
workFileContents = workFile.readline()
print(workFileContents)
workFile.close()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
4
Q

In the following example, the file ‘log.txt’ does not exist on the local hard drive.

Will this script create a file or not?

writeFile = open(‘log.txt’, ‘w’)
toLog = input(‘What do you want to write to the log?’ )
writeFile.write(tolog)
writeFile.close()

A

Yes, the script will create a file if one does not exist.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
5
Q

If you have previously created the file in the example below, and decide to rerun the script to add more content, what will happen?

writeFile = open(‘log.txt’, ‘w’)
toLog = input(‘What do you want to write to the log?’ )
writeFile.write(tolog)
writeFile.close()

A

The contents of the file will be rewritten upon each execution of the code.

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
6
Q

Python has a library called import os which is short for operating system

  1. List some of the most common functions that this library contains.
A
  1. It can be used to check if a file exists or not using os.path.isfile(‘log,txt’)
How well did you know this?
1
Not at all
2
3
4
5
Perfectly
7
Q

The following code example is missing one line. The objective is to delete the file ‘log_old.txt’

What is the missing line of code?

import os
if os.path.isfile(‘log_old.txt’):
missing code
print(‘The log_old file has been removed’)
else:
print(‘There was no log_old file to remove)

A

import os
if os.path.isfile(‘log_old.txt’):
_os.remove(‘log_old.txt’)_
print(‘The log_old file has been removed’)
else:
print(‘There was no log_old file to remove)

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
8
Q

In the following code example, what purpose does the “with” statement serve?

with open(‘log.txt’, ‘w’) as writeFile:
toLog = input(‘What do you want to write to the log? ‘)
writeFile.write(toLog)

A

The ‘with’ statement ensures the file can be properly closed. It also removes the need to write the previous close way of writeFile.close()

How well did you know this?
1
Not at all
2
3
4
5
Perfectly
9
Q

WIP - EXPLORE ALL THE DIFFERENT WAYS TO DO FORMATTED TEXT

A

WIP - EXPLORE ALL THE DIFFERENT WAYS TO DO FORMATTED TEXT

How well did you know this?
1
Not at all
2
3
4
5
Perfectly