Here are the steps to write Python code to read a file and display its characteristics regarding type and size:
import os
path = "/path/to/your/file"
if os.path.exists(path):
file_size = os.path.getsize(path)
print("File size:", file_size, "bytes")
else:
print("File not found")
if os.path.isfile(path):
file_extension = os.path.splitext(path)[1]
print("File extension:", file_extension)
if file_extension == ".txt":
print("File type: Text file")
elif file_extension in [".csv", ".xls", ".xlsx"]:
print("File type: Spreadsheet")
elif file_extension in [".jpg", ".png", ".gif"]:
print("File type: Image")
else:
print("Unknown file type")
else:
print("Path is not a file")
Note: Make sure to change the path variable to the actual file path in your system before running the program.
Asked: 2021-08-27 11:00:00 +0000
Seen: 7 times
Last updated: May 02 '22