Syntasa provides a set of utilities for interacting with the filesystem through Python and Scala code. These utilities offer functionalities like listing files, reading file contents, copying, moving, uploading, creating directories, renaming files, and checking existence.
1. Listing Files using ls
The ls function retrieves a list of files and directories within a specified path.
Python
/***
*
* @param filepath
* @return listfiles as string
*/
listfiles = synutils.fs().ls(filepath)
Example:
listfiles = synutils.fs().ls("test")
2. Reading File Content using content
The content function reads the content of a file.
/***
*
* @param path and filename
* @return filecontent
*/
filecontent = synutils.fs().content(path,filename)
Example:
content = synutils.fs().content("PATH/TO/FILE/test/","attrition_test.csv")
This example reads the contents of the file "attrition_test.csv" located in the "test" directory and stores it in the content variable.
Scala
/***
*
* @param path and filename
* @return String
*/
val filecontent = synutils.fs().content(path,filename)
Example:
val content = synutils.fs().content("test/attrition_test.csv","");
3. Copying Files using copy
The copy function replicates a file from one location to another.
Python
/***
*
* @param sourcepath and destinationpath
*/
synutils.fs().copy(source,destination)
Example:
synutils.fs().copy("test/scala.txt","tmp/scaladummy/")
4. Moving Files using mv
The mv the function acts similarly to the copy function but removes the source file after the copy operation.
Python
/***
*
* @param sourcepath and destinationpath
*/
synutils.fs().mv(source,destination)
Example:
synutils.fs().copy("test/scala.txt","tmp/scaladummy/")
This code snippet moves the file "scala.txt" from the "test" directory to the "tmp/scaladummy/" directory, deleting the source file after the move.
Scala
/***
*
* @param sourcepath and destinationpath
*/
synutils.fs().mv(source,destination)
Example:
synutils.fs().copy("test/scala.txt","tmp/scaladummy/")
5. Uploading Files using put
The put function uploads a file to a specified location.
Python
/***
*
* @param upload file and destinationpath
*/
synutils.fs().put(uploadFileName,destinationFilePath)
Example:
file = open("abc.txt", "w")
file.write("Your text goes here")
file.close()
synutils.fs().put("abc.txt","test/")
This example creates a file named "abc.txt" with content "Your text goes here" and uploads it to the "test" directory.
Scala
/***
*
* @param upload file and destinationpath
*/
synutils.fs().put(uploadFileName,destinationFilePath)
Example:
import java.io._
val pw = new PrintWriter(new File("abc.txt" ))
pw.write("Hello, world")
pw.close
synutils.fs().put("abc.txt","test/")
6. Creating Directories using mkdir
The mkdir function creates a new directory.
Python
/***
*
* @param dir
*/
synutils.fs().mkdir(dir)
Example:
synutils.fs().mkdir("test/scaladummy");
This code creates a new directory named "scaladummy" inside the "test" directory.
Scala
/***
*
* @param dir
*/
synutils.fs().mkdir(dir)
Example:
synutils.fs().mkdir("test/scaladummy");
7. Renaming Files using rename
The rename function changes the name of a file.
Python
/***
*
* @param oldpath and newpath
*/
synutils.fs().rename(oldPath,newPath)
Example:
synutils.fs().rename("test/scaladummy/scala.txt","test/scaladummy/scala_rename.txt")
This code renames the file "scala.txt" to "scala_rename.txt" within the "test/scaladummy" directory.
Scala
/***
*
* @param oldpath and newpath
*/
synutils.fs().rename(oldPath,newPath)
Example:
synutils.fs().rename("test/scaladummy/scala.txt","test/scaladummy/scala_rename.txt")
8. Checking Existence using exist
The exist function verifies if a file or directory exists.
Python
/***
*
* @param filePath
*/
synutils.fs().exist(filePath)
Example:
exist = synutils.fs().exist("test/scaladummy/scala.txt")
This code checks if the file "scala.txt" exists in the "test/scaladummy" directory and assigns the result (True or False) to the exist variable.
Scala
/***
*
* @param filePath
* @return boolean
*/
synutils.fs().exist(filePath)
Exmaple:
val exist = synutils.fs().exist("test/scaladummy/scala.txt")