Check Whether a Path Is Valid

Check if a string is a valid Windows directory (folder) path

Call Path.GetFullPath; it will throw exceptions if the path is invalid.

To disallow relative paths (such as Word), call Path.IsPathRooted.

Check whether a path is valid in Python

Attempting it first is the best way, I recommend doing that.

try:
open(filename, 'w')
except OSError:
# handle error here

I believe you'll get OSError, catch that explicitly, and test on the platform you're using this on.

Is there a way in Java to determine if a path is valid without attempting to create a file?

This would check for the existance of the directory as well.

File file = new File("c:\\cygwin\\cygwin.bat");
if (!file.isDirectory())
file = file.getParentFile();
if (file.exists()){
...
}

It seems like file.canWrite() does not give you a clear indication if you have permissions to write to the directory.

Determine via C# whether a string is a valid file path

A 100% accurate checking of a path's string format is quite difficult, since it will depend on the filesystem on which it is used (and network protocols if its not on the same computer).

Even within windows or even NTFS its not simple since it still depends on the API .NET is using in the background to communicate with the kernel.

And since most filesystems today support unicode, one might also need to check for all the rules for correcly encoded unicode, normalization, etc etc.

What I'd do is to make some basic checks only, and then handle exceptions properly once the path is used. For possible rules see:

  • Wikipedia - Filename for an overview of the rules used by different file systems
  • Naming Files, Paths, and Namespaces for windows specific rules

How to check if path is valid without using try-catch?

The explanation for the failure of your code is that the path is invalid. The documentation says:

DirectoryNotFoundException

The specified path is invalid (for example, it is on an
unmapped drive).

Trying to predict in advance whether or not a directory can be created is a devil of a job. You'd need to account for security, OS name rules and limits, file system name rules and limits, and whether or not drives are mapped. And probably lots more concerns. I would not contemplate re-implementing what the system provides for free.

In any case, whilst you can call Directory.Exists, you do still need to allow for an exception being thrown. If the file system changes between the call to Directory.Exists and the subsequent call to Directory.CreateDirectory, then an exception will be raised. For example, if another process creates the directory that you are trying to create. Granted, this is a rather unlikely event, but it's perfectly possible.

In summary, the best option, by a distance, is to catch the exception. As the well known saying goes, it's better to ask for forgiveness than to ask for permission.

Check if a path is valid, even if it doesn't exist

You gain nothing by doing (pseudo-code alert):

if (doesnt_exist(dir)) {
create_directory(dir);
}

Better to just do:

create_directory(dir);

And handle errors properly if necessary. You may not even need to handle errors here if you handle subsequent errors.

The "if exists" check introduces unnecessary complexity, and introduces a race condition. And even if the directory doesn't exist, you may still not have permission to create it. So in any case your error handling will be the same, rendering the "if exists" check completely pointless.

I guess it's another way of "don't ask permission, ask forgiveness" except with computers you don't need to ask forgiveness.



Related Topics



Leave a reply



Submit