remove whitespace from directory path

Collapse
X
 
  • Time
  • Show
Clear All
new posts
  • fahadqureshi
    New Member
    • Oct 2007
    • 28

    remove whitespace from directory path

    i am trying to get a user input and use that input as part of a directory path. i have tried many way of doing this but cant get rid of the space.

    Code:
    accfile1 = raw_input("Enter file name:")
    print "C:\Fahad\ACC\Output\ ".replace(' ', ''),accfile1
    My output looks like this
    C:\Fahad\ACC\Ou tput\ test.txt

    notice the white space before test.txt. I want it to look like this
    C:\Fahad\ACC\Ou tput\test.txt

    I have tried the replace method as shown and a couple other methods i found online but the annoying white space wont go away.

    any help would be greatly appreciated
  • bvdet
    Recognized Expert Specialist
    • Oct 2006
    • 2851

    #2
    The problem is not the space, but the backslash character (\). It is used to escape special characters such as newlines and tabs. Example:
    Code:
    >>> print 'abc\test\Bxxx\n1234'
    abc	est\Bxxx
    1234
    >>> print 'abc\\test\\Bxxx\\n1234'
    abc\test\Bxxx\n1234
    >>>
    For a literal backslash, escape it with "\\" or input forward slashes.

    -BV

    Comment

    • fahadqureshi
      New Member
      • Oct 2007
      • 28

      #3
      what if i was trying to use the string as a path name. is it even possible to store a pathname as a string. for example if i wanted to assign C:\fahad\test.t xt as a string and then call on it.

      an overall picture of what i am trying to do is ask the user for a file name at which the user will enter for example "test.txt" . it may not be .txt everytime could be other file names whatever the user enters.

      now i want to use what the user entered as part of the pathname and store the entire pathname in one variable.

      Code:
      accfile = raw_input("Enter file name: ")
      path =  str("C:\Fahad\ACC\Output\\"), accfile
      print path
      so if the user enters test.txt as the raw input

      the output is coming out as: ('C:\\Fahad\\AC C\\Output\\', 'test.txt')
      instead i need it to come out clean as "C:\Fahad\ACC\O utput\test.txt"

      it looks like whats after the comma is being considered as separate string wheras i want it to be part of the original string.

      Comment

      • bvdet
        Recognized Expert Specialist
        • Oct 2006
        • 2851

        #4
        The strings must use "\\" or "/" to represent a valid path. Try using os.path.join() or string method join(). Example:
        Code:
        >>> import os
        >>> dir_path = "C:\\Fahad\\ACC\\Output"
        >>> accfile = raw_input("Enter file name: ")
        >>> print accfile
        test.txt
        >>> os.path.join(dir_path, accfile)
        'C:\\Fahad\\ACC\\Output\\test.txt'
        >>> print os.path.join(dir_path, accfile)
        C:\Fahad\ACC\Output\test.txt
        >>> dir_path = "C:/Fahad/ACC/Output"
        >>> '/'.join([dir_path, accfile])
        'C:/Fahad/ACC/Output/test.txt'
        >>>

        Comment

        • fahadqureshi
          New Member
          • Oct 2007
          • 28

          #5
          Thanks a bunch

          Thanks a lot man it worked, you are incredible at this.

          Comment

          • bvdet
            Recognized Expert Specialist
            • Oct 2006
            • 2851

            #6
            You are welcome. Keep posting.

            -BV

            Comment

            Working...