Working with Strings

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • ncharman
    New Member
    • Oct 2008
    • 3

    Working with Strings

    Hello all, I am new to python and in need of some aide. I am going through some Homework and am completely stumped and unable to find answers in any tutorials that are in plain enough english that I can understand.

    my code is so far as followed:
    Code:
    def GetFileExtension(fullFileName):
        print fullFileName[-4:]
        
    def GetBaseName(fullFileName):
        print fullFileName[:-4]
    
    GetFileExtension("C:/MyFolder/Docs/Notes.txt")
    GetBaseName("C:/MyFolder/Docs/Notes.txt")
    Thus far the code will run and shows what is explained in the question, however; if the file extension was for example a .jpeg it would not work properly.

    question for this question is as followed:

    1. The “\” in Windows file paths are represented by “\\” or “/” in Python. Create a FileInfo.py and create two functions: GetFileExtensio n(fullFileName) and GetBaseFileName (fullFileName). Given “C:/MyFolder/Docs/Notes.txt”, GetFileExtensio n will return “.txt” and GetBaseName will return “C:/MyFolder/Docs/Notes”. At the top of FileInfo.py, write the code for these two functions. At the bottom of FileInfo.py, write calls to both of these functions using “C:/MyFolder/Docs/Notes.txt” as the argument.
    HINTS: You will be using slicing and the len() function to perform this task.


    any aide would be greatly appreciated.
  • boxfish
    Recognized Expert Contributor
    • Mar 2008
    • 469

    #2
    To give you a general idea of what you need to do, you'll be starting at the end of the filename string and going backward until you find the last dot in the string. Then you slice it off. You should be able to do this with a loop. I hope this helps a bit.

    Comment

    • asedt
      New Member
      • Jun 2008
      • 130

      #3
      Originally posted by ncharman
      HINTS: You will be using slicing and the len() function to perform this task.

      Hmm, I wold do it with Regular Expressions, but if the task is to do it with slicing and len() hmm.. I don't know how to do it.

      But now you are using slice only so I guess the solution is in knowing how to use len() in the solution.


      edit:

      I think you can slice the last part of the string, slice the first letter of that string and se if its a dot. And start with slicing 1 char and then add 1 char mor to slice from the original string until the first char is a dot. (Then I think you need to use len() to)

      Comment

      • boxfish
        Recognized Expert Contributor
        • Mar 2008
        • 469

        #4
        You just start at the end of the string (find it with len). Is it a dot? No. Then step back one character. Dot? No. Go back. Is it a dot? Yes. Now you have the index of the dot character. Slice off everything after that.
        Hope this makes sense.

        Comment

        Working...