How do I check if a path / file exists in Scala similar to Python ? An example below:
os.path.exists(\"/home\")
Out[4]: True
It is an old question, but I still need it needs some update. You should use isFile
or isRegularFile
instead of exists
since exists
don´t take in account if is a File or a Directory and can mislead the application in case there is a directory with the same name.
Using java.io
new java.io.File("/tmp/sample.txt").isFile
Using java.nio
java.nio.file.Files.isRegularFile(java.nio.file.Paths.get("/tmp/sample.txt"))