To check if a file exists, you pass the file path to the exists() function from the os. path standard library. If the file exists, the exists() function returns True . Otherwise, it returns False .
To test to see if a file or directory exists, use the exists method of the Java File class, as shown in this example: File tmpDir = new File("/var/tmp"); boolean exists = tmpDir. exists(); The existing method of the Java File class returns true if the file or directory exists, and false otherwise.
Python check If a file exists The module path checks the specified path is present or not. I have used a function called main as def main(). The path. exists() is used to check whether the specified path exists or not.
This is a way to see if any XML-files exists in that folder, yes.
To check for specific files use File.Exists(path)
, which will return a boolean indicating wheter the file at path
exists.
Use FileInfo.Exists
Property:
DirectoryInfo di = new DirectoryInfo(ProcessingDirectory);
FileInfo[] TXTFiles = di.GetFiles("*.xml");
if (TXTFiles.Length == 0)
{
log.Info("no files present")
}
foreach (var fi in TXTFiles)
log.Info(fi.Exists);
or File.Exists
Method:
string curFile = @"c:\temp\test.txt";
Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
To check file exists or not you can use
System.IO.File.Exists(path)
Since nobody said how to check if the file exists AND get the current folder the executable is in (Working Directory):
if (File.Exists(Directory.GetCurrentDirectory() + @"\YourFile.txt")) {
//do stuff
}
The @"\YourFile.txt"
is not case sensitive, that means stuff like @"\YoUrFiLe.txt"
and @"\YourFile.TXT"
or @"\yOuRfILE.tXt"
is interpreted the same.
This way we can check for an existing file in a particular folder:
string curFile = @"c:\temp\test.txt"; //Your path
Console.WriteLine(File.Exists(curFile) ? "File exists." : "File does not exist.");
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With