0
0
Intro to Computingfundamentals~10 mins

File system management in Intro to Computing - Interactive Code Practice

Choose your learning style9 modes available
Practice - 5 Tasks
Answer the questions below
1fill in blank
easy

Complete the code to open a file named 'data.txt' for reading.

Intro to Computing
file = open('data.txt', '[1]')
Drag options to blanks, or click blank then click option'
Aw
Br
Cx
Da
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'w' mode which erases the file content.
Using 'x' which is not a mode for reading.
2fill in blank
medium

Complete the code to write the string 'Hello' into a file.

Intro to Computing
with open('greeting.txt', 'w') as file:
    file.[1]('Hello')
Drag options to blanks, or click blank then click option'
Awrite
Bread
Cappend
Dclose
Attempts:
3 left
💡 Hint
Common Mistakes
Using 'read' which only reads content.
Using 'close' which closes the file but does not write.
3fill in blank
hard

Fix the error in the code to properly close the file after reading.

Intro to Computing
file = open('info.txt', 'r')
content = file.read()
file.[1]()
Drag options to blanks, or click blank then click option'
Aopen
Bwrite
Cclose
Dread
Attempts:
3 left
💡 Hint
Common Mistakes
Calling 'open' again instead of closing.
Trying to 'write' when only reading.
4fill in blank
hard

Fill both blanks to create a dictionary with file names as keys and their sizes in bytes as values.

Intro to Computing
file_sizes = {filename: [1] for filename in files if [2]
Drag options to blanks, or click blank then click option'
Aos.path.getsize(filename)
Bfilename.endswith('.txt')
Cfilename.startswith('doc')
Dlen(filename)
Attempts:
3 left
💡 Hint
Common Mistakes
Using length of filename instead of file size.
Filtering files incorrectly by start instead of end.
5fill in blank
hard

Fill all three blanks to create a dictionary with uppercase file names as keys and their last modified time if size is greater than 1000 bytes.

Intro to Computing
file_info = [1]: os.path.getmtime([2]) for [3] in files if os.path.getsize([3]) > 1000
Drag options to blanks, or click blank then click option'
Afilename.upper()
Bfilename
Dfile
Attempts:
3 left
💡 Hint
Common Mistakes
Using different variable names inconsistently.
Not converting filename to uppercase for keys.