📖 Scenario: You are managing files on your computer. Each file has details like its name, size, and permissions. You want to organize this information clearly to understand and control your files better.
🎯 Goal: Create a simple list of files with their attributes, add a filter to find files larger than a certain size, and then list those files with their details.
📋 What You'll Learn
Create a list called files containing dictionaries for each file with keys: 'name', 'size' (in KB), and 'permissions' (a string like 'rwx').
Create a variable called size_limit set to 100 to filter files larger than 100 KB.
Use a list comprehension to create a new list called large_files containing only files from files with 'size' greater than size_limit.
Add a final step to print the names and sizes of the files in large_files.
💡 Why This Matters
🌍 Real World
Organizing and managing files on a computer by checking their size and permissions helps keep data safe and easy to find.
💼 Career
Understanding file attributes and how to filter or list files is important for system administrators, IT support, and anyone managing computer storage.
Progress0 / 4 steps
1
Create the initial list of files with attributes
Create a list called files with these exact dictionaries: {'name': 'report.docx', 'size': 120, 'permissions': 'rw-'}, {'name': 'photo.jpg', 'size': 85, 'permissions': 'r--'}, and {'name': 'script.py', 'size': 150, 'permissions': 'rwx'}.
Operating Systems
Hint
Use a list with dictionaries. Each dictionary must have keys 'name', 'size', and 'permissions' with the exact values given.
2
Set the size limit for filtering files
Create a variable called size_limit and set it to 100 to use as the threshold for filtering files by size.
Operating Systems
Hint
Just create a variable named size_limit and assign it the number 100.
3
Filter files larger than the size limit
Use a list comprehension to create a new list called large_files that includes only the dictionaries from files where the 'size' value is greater than size_limit.
Operating Systems
Hint
Use a list comprehension with file for file in files if file['size'] > size_limit.
4
Display the names and sizes of large files
Add a for loop to go through large_files and print each file's 'name' and 'size' in the format: File: <name>, Size: <size>KB.
Operating Systems
Hint
Use a for loop with file as the variable and print using an f-string.
Practice
(1/5)
1. Which of the following is NOT a common file attribute in operating systems?
easy
A. File color
B. File size
C. Creation date
D. File permissions
Solution
Step 1: Understand common file attributes
Common file attributes include size, creation date, and permissions which describe the file's properties.
Step 2: Identify the incorrect attribute
File color is not a standard attribute tracked by operating systems; it is usually a display feature in some file managers.
Final Answer:
File color -> Option A
Quick Check:
File color is not a file attribute [OK]
Hint: File attributes describe file details, not display colors [OK]
Common Mistakes:
Confusing display features with file attributes
Thinking file color is stored as metadata
2. Which command is used to change file permissions in Unix-like systems?
easy
A. ls
B. chown
C. chmod
D. rm
Solution
Step 1: Recall commands related to file permissions
chmod is the command used to change file permissions, allowing read, write, and execute rights to be set.
Step 2: Differentiate from other commands
chown changes file ownership, ls lists files, and rm deletes files, so they do not change permissions.
Final Answer:
chmod -> Option C
Quick Check:
chmod changes permissions [OK]
Hint: chmod = change mode (permissions) [OK]
Common Mistakes:
Confusing chmod with chown
Using ls or rm for permissions
3. Consider this Python code snippet that reads a file's size:
import os
file_path = 'example.txt'
size = os.path.getsize(file_path)
print(size)
What does this code output?
medium
A. The number of lines in the file
B. The size of the file in bytes
C. The file's creation date
D. The file's permissions
Solution
Step 1: Understand os.path.getsize function
This function returns the size of the file in bytes, not lines or dates.
Step 2: Analyze the print output
Printing the size variable outputs the file size in bytes as an integer.
Final Answer:
The size of the file in bytes -> Option B
Quick Check:
os.path.getsize returns file size [OK]
Hint: getsize returns bytes, not lines or dates [OK]
Common Mistakes:
Thinking it counts lines
Confusing size with creation date
4. What is wrong with this command to rename a file in Linux?
mv file1.txt file2.txt/
medium
A. The trailing slash after file2.txt treats it as a directory
B. It will delete file1.txt instead of renaming
C. The command syntax is incorrect; mv needs -r option
D. It will copy file1.txt instead of renaming
Solution
Step 1: Understand mv command syntax
mv renames or moves files. The destination should be a file name or directory.
Step 2: Analyze the trailing slash effect
A trailing slash after file2.txt makes the system treat it as a directory, which likely does not exist, causing an error.
Final Answer:
The trailing slash after file2.txt treats it as a directory -> Option A
Quick Check:
Trailing slash means directory [OK]
Hint: Trailing slash means directory, not file [OK]
Common Mistakes:
Thinking mv deletes files
Assuming mv copies files
Believing -r is needed for mv
5. You want to create a script that lists all files in a folder but only shows files larger than 1MB. Which approach correctly uses file attributes and operations?
hard
A. Change permissions of files larger than 1MB before listing
B. List all files and print their names without checking size
C. Delete files smaller than 1MB and then list remaining files
D. List all files, then check each file's size attribute and print only those >1MB
Solution
Step 1: Understand the requirement
The task is to list files filtered by size, so size attribute must be checked.
Step 2: Evaluate each option
List all files, then check each file's size attribute and print only those >1MB correctly filters files by size before printing. List all files and print their names without checking size ignores size. Delete files smaller than 1MB and then list remaining files deletes files, which is unsafe. Change permissions of files larger than 1MB before listing changes permissions unnecessarily.
Final Answer:
List all files, then check each file's size attribute and print only those >1MB -> Option D
Quick Check:
Filter by size attribute before listing [OK]
Hint: Filter files by size attribute before listing [OK]
Common Mistakes:
Ignoring file size attribute
Deleting files instead of filtering
Changing permissions unnecessarily
Recommended
Ready to prove it?
Pass the test and this pattern moves unknownstrongwhile earning +1 Grip toward your certificate.