Easy way to work with files, directories and paths in swift on macOS and linux.
You are developing a cli and you want to:
- Read/Write files.
- Create/Delete files.
- Create/Delete/List directories.
- Get different paths (Home/Current/Temp).
- Get the base name and directory name of a path
You can use
FileUtils with Guaka to create aweseome command line applications.
Note: At the moment, this library only deals with textual files contents. (Check todo section of this file).
Create a file
File(path: path, fileMode: .write) // or File.create(path: path)
Delete a file
Read file content
let content = File.read(atPath: path) // or let content = String.read(contentsOfFile: path)
Write file content
File.write(string: "ABCDEF", toPath: path, ) // or "AAAAA".write(toFile: path)
Check if file exists
Get temporary file and directory
let tmp = Path.tempPath // or let tmp = Path.tempFile // or let tmp = Path.tempFileName(withName: "abc.txt")
Get the current directory
let path = Path.currentDirectory
Get the home directory
let path = Path.home
Check if the path exists
let exists = Path.exists(path)
Check the type of the file at a path
let type = Path.type(ofPath: path)
type is a member of the
PathType enum. This enum defines
Expand a tilde in the path
let expanded = Path.expand("~/Documents") // expanded is "/Users/YourUser/Documents"
Get the base name and the directory name of a path
let base = Path.baseName(forPath: "/Documents/this/is/mypath") // base is "mypath" let dir = Path.dirName(forPath: "/Documents/this/is/mypath") // dir is "/Documents/this/is"
Create a directory
Delete a directory
Enumerate contents of a directory
let (files, directories) = Directory.contents(ofDirectory: path)!
this returns a tuple that contains all the files and directories found at the path
You can install File using Swift package manager (SPM) and Carthage
Swift Package Manager
Add FileSystem as a dependency in your
import PackageDescription let package = Package(name: "YourPackage", dependencies: [ .Package(url: "https://github.com/getGuaka/FileUtils.git", majorVersion: 0), ] )
Tests can be found here.
Run them with
- Handle non textual files contents
Just send a PR! We don't bite ;)