I am trying to port some Python code to .NET, and I was wondering if there were equivalents of the following Python functions in .NET, or some code snippets that have the same functionality.
os.path.split()
os.path.basename()
Edit
os.path.basename() in Python returns the tail of os.path.split, not the result of System.IO.Path.GetPathRoot(path)
I think the following method creates a suitable port of the os.path.split function, any tweaks are welcome. It follows the description of os.path.split from http://docs.python.org/library/os.path.html as much as possible I believe.
public static string[] PathSplit(string path)
{
string head = string.Empty;
string tail = string.Empty;
if (!string.IsNullOrEmpty(path))
{
head = Path.GetDirectoryName(path);
tail = path.Replace(head + "\\", "");
}
return new[] { head, tail };
}
I'm unsure about the way I'm returning the head and tail, as I didn't really want to pass out the head and tail via parameters to the method.