tags:

views:

130

answers:

4

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.

+3  A: 

You're looking for the System.IO.Path Class.

It has many functions you can use to get the same functionality.

  • Path.GetDirectoryName(string)
  • For split, you'll probably want to use String.Split(...) on the actual path name. You can get the OS Dependant seperator by Path.PathSeparator.
  • In the case that im missing the point about os.path.split and you want the file name, use Path.GetFileName(string).

Please Note: You can explore all the members of the System.IO namespace by using the Object Browser (Ctrl+Alt+J) in Visual Studio. From here you go mscorlib -> System.IO and all the classes will be discoverable there.

It's like Intellisense on crack :)

Aren
AFAIK `os.path.split()` is not the same as `String.Split()`. `os.path.split()` breaks a path down into two parts (head and tail). I.e. C:/Folder/File.txt would be broken down into C:/Folder and File.txt. But I may be wrong as I may have misunderstood the docs. I'm no python programmer.
GenericTypeTea
@GenericTypeTea: Hrm you're probably right. It's been a while since i did anything in python. I'll amend to my answer.
Aren
@Aren - GetFileName still isn't similar to os.path.split() will also return the last folder in a path. I.e. C:\Foo\Bar would be C:\Foo, Bar.
GenericTypeTea
@GenericTypeTea: There is no direct equivalent to `os.path.split()` then. You'll have to use `.GetFileName(string)` and `.GetDirectoryName(string)` to get the two parts.
Aren
@Aren - `os.path.split()` gives you a head and a tail of the path. It does not JUST give you the filename. If there's no filename then it gives you the last folder in the path. I.e. like I said in my previous comment, it should return head: C:\Foo and tail: Bar from the example C:\Foo\Bar. So, GetFileName is not the equivalent.
GenericTypeTea
A: 

Path.GetFileName Path.GetDirectoryName

http://msdn.microsoft.com/en-us/library/beth2052.aspx Should help

b8b8j
+1  A: 
GenericTypeTea
Aren
@Aren B - Actually, you should use the `DirectorySeparatorChar` as `PathSeparator` is probably something like `;`.
GenericTypeTea
A: 

Why not just use IronPython and get the best of both worlds?

Mike Driscoll