Getting a extension of a given filename

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • psbasha
    Contributor
    • Feb 2007
    • 440

    Getting a extension of a given filename

    Hi,

    I need to extract the filename extension from the given filename at a particular location

    I/P:
    say 'C:\Sample\Samp le1.txt'

    O/P :
    The file etension is '.txt'

    Thanks
    PSB
  • Motoma
    Recognized Expert Specialist
    • Jan 2007
    • 3236

    #2
    [code=python]
    def findExt(filenam e):
    return filename.rparti tion(".")[2]
    [/code]

    Comment

    • psbasha
      Contributor
      • Feb 2007
      • 440

      #3
      Originally posted by Motoma
      [code=python]
      def findExt(filenam e):
      return filename.rparti tion(".")[2]
      [/code]
      Is it a filepointer or a Filename path?

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        Code:
        >>> import os
        >>> p = r'C:\SDS2_7.0\macro\Zip\tem_v1.00.txt'
        >>> os.path.splitext(p)
        ('C:\\SDS2_7.0\\macro\\Zip\\tem_v1.00', '.txt')
        >>>

        Comment

        • Motoma
          Recognized Expert Specialist
          • Jan 2007
          • 3236

          #5
          Originally posted by psbasha
          Is it a filepointer or a Filename path?
          The filename variable is a string.

          Comment

          • ghostdog74
            Recognized Expert Contributor
            • Apr 2006
            • 511

            #6
            Originally posted by psbasha
            Is it a filepointer or a Filename path?
            you should also note that rpartition is available in Python 2.5. if earlier, use bv's method, or use the usual split way:
            Code:
            "c:\\test.txt.txt".split(".")[-1]

            Comment

            • Motoma
              Recognized Expert Specialist
              • Jan 2007
              • 3236

              #7
              Originally posted by bvdet
              Code:
              >>> import os
              >>> p = r'C:\SDS2_7.0\macro\Zip\tem_v1.00.txt'
              >>> os.path.splitext(p)
              ('C:\\SDS2_7.0\\macro\\Zip\\tem_v1.00', '.txt')
              >>>
              Thanks bvdet. This would be the preferable way of doing it, in my opinion, as it is built into the language.
              Last edited by Motoma; May 16 '07, 12:38 PM. Reason: I fail at using the English langUage.

              Comment

              • ghostdog74
                Recognized Expert Contributor
                • Apr 2006
                • 511

                #8
                Originally posted by Motoma
                Thanks bvdet. This would be the preferable way of doing it, in my opinion, as it is built into the langage.
                hmm..I cannot say if its "built in" (as in really built in , like str(), int) because to use os.path.splitex t, we need to import os. but to use rpartition, as its a string method, just need "path".rpartiti on("") , much like split() method :)

                Comment

                • Motoma
                  Recognized Expert Specialist
                  • Jan 2007
                  • 3236

                  #9
                  Originally posted by ghostdog74
                  hmm..I cannot say if its "built in" (as in really built in , like str(), int) because to use os.path.splitex t, we need to import os. but to use rpartition, as its a string method, just need "path".rpartiti on("") , much like split() method :)
                  I agree with what you are saying; I guess what I was getting at is that the os.path.splitex t was designed specifically for this purpose, whilst rpartition has a variety of uses.

                  Comment

                  • pyarticles
                    New Member
                    • May 2007
                    • 2

                    #10
                    Check out this Python Articles

                    Comment

                    Working...