find the size of a csv file in swift

You can find the size of a csv file using FileManager in Swift by following these steps:

  1. Get the file path of the csv file.
  2. Get the attributes of the file using FileManager.
  3. Retrieve the file size attribute from the attributes dictionary.

Here's the code:

main.swift
let filePath = "/path/to/csv/file.csv"

do {
    let attributes = try FileManager.default.attributesOfItem(atPath: filePath)
    let fileSize = attributes[.size] as! Int64
    let sizeInMB = Double(fileSize) / (1024 * 1024)
    print("File size: \(sizeInMB) MB")
} catch {
    print("Error: \(error)")
}
304 chars
11 lines

In the code above, we first define filePath as the path to the csv file. Then, we try to get the attributes of the file using FileManager. If successful, we retrieve the size attribute from the attributes dictionary and cast it to an Int64. Finally, we convert the size to MB and print it to the console.

related categories

gistlibby LogSnag