Complete the code to identify the file type by its extension.
file_name = "document[1]" if file_name.endswith('.txt'): print("This is a text file.")
The .txt extension indicates a text file. The code checks if the file name ends with .txt to identify it.
Complete the code to open an image file based on its extension.
file_name = "photo[1]" if file_name.endswith('.png') or file_name.endswith('.jpg'): print("Open image viewer")
The .jpg extension is a common image file type. The code checks if the file ends with .jpg or .png to open it with an image viewer.
Fix the error in the code to correctly associate a file type with its program.
file_extension = '.pdf' if file_extension == [1]: print("Open with PDF reader")
The file extension includes the dot, so it must be '.pdf' to match the variable file_extension.
Fill both blanks to create a dictionary mapping file extensions to their associated programs.
file_associations = { [1]: "Text Editor", [2]: "Image Viewer" }The dictionary maps ".txt" to a text editor and ".jpg" to an image viewer, matching common file type associations.
Fill all three blanks to filter file names by extension and create a list of those files.
files = ["doc1.txt", "image.png", "song.mp3", "notes.txt"] txt_files = [f for f in files if f.endswith([1]) or f.endswith([2])] print(txt_files) # Should print files ending with [3]
The code filters files ending with .txt to create a list of text files. Both blanks use ".txt" to check the extension, and the comment confirms this.