Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 969555
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T02:39:36+00:00 2026-05-16T02:39:36+00:00

I am trying to port some Python code to .NET, and I was wondering

  • 0

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.

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-16T02:39:37+00:00Added an answer on May 16, 2026 at 2:39 am

    os.path.basename()

    The alternative is System.IO.Path.GetPathRoot(path);:

    System.IO.Path.GetPathRoot("C:\\Foo\\Bar.xml") // Equals C:\\
    

    Edit: The above returned the first path of the path, where basename should return the tail of the path. See the code below for an example of how this could be achieved.

    os.path.split()

    Unfortunately there’s no alternative to this as there’s no .Net equivalent. The closest you can find is System.IO.Path.GetDirectoryName(path), however if your path was C:\Foo, then GetDirectoryName would give you C:\Foo instead of C: and Foo. This would only work if you wanted to get the Directory Name of an actual file path.

    So you’ll have to write some code like the following to break these down for you:

    public void EquivalentSplit(string path, out string head, out string tail)
    {
    
        // Get the directory separation character (i.e. '\').
        string separator = System.IO.Path.DirectorySeparatorChar.ToString();
    
        // Trim any separators at the end of the path
        string lastCharacter = path.Substring(path.Length - 1);
        if (separator == lastCharacter)
        {
            path = path.Substring(0, path.Length - 1);
        }
    
        int lastSeparatorIndex = path.LastIndexOf(separator);
    
        head = path.Substring(0, lastSeparatorIndex);
        tail = path.Substring(lastSeparatorIndex + separator.Length,
            path.Length - lastSeparatorIndex - separator.Length);
    
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I am trying to port some code form java do F# that generates a
I'm trying to port some of my c++ code into c. I have the
I'm trying to figure out some C code so that I can port it
I'm trying to port some code to 64-bit, but it seems that the thread
I have some Python code I'm trying to translate to Obj-C/Cocoa. It requires a
I am trying to port some Python code and I am a little lost
I am trying to port some code from Delphi to C# and I have
I am tryiing to port some python code to ruby, and I am doing
I'm trying to port some Windows code to Linux, ideally through platform-independent libraries (eg
I am trying to port some code using DevExpress XtraGrid to Wpf. What is

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.