Connecting Tech Pros Worldwide Help | Site Map

remove whitespace from directory path

Newbie
 
Join Date: Oct 2007
Posts: 28
#1: Jun 11 '09
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.

Expand|Select|Wrap|Line Numbers
  1. accfile1 = raw_input("Enter file name:")
  2. print "C:\Fahad\ACC\Output\ ".replace(' ', ''),accfile1
My output looks like this
C:\Fahad\ACC\Output\ test.txt

notice the white space before test.txt. I want it to look like this
C:\Fahad\ACC\Output\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's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#2: Jun 11 '09

re: remove whitespace from directory path


The problem is not the space, but the backslash character (\). It is used to escape special characters such as newlines and tabs. Example:
Expand|Select|Wrap|Line Numbers
  1. >>> print 'abc\test\Bxxx\n1234'
  2. abc    est\Bxxx
  3. 1234
  4. >>> print 'abc\\test\\Bxxx\\n1234'
  5. abc\test\Bxxx\n1234
  6. >>> 
For a literal backslash, escape it with "\\" or input forward slashes.

-BV
Newbie
 
Join Date: Oct 2007
Posts: 28
#3: Jun 11 '09

re: remove whitespace from directory path


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.txt 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.

Expand|Select|Wrap|Line Numbers
  1. accfile = raw_input("Enter file name: ")
  2. path =  str("C:\Fahad\ACC\Output\\"), accfile
  3. print path
so if the user enters test.txt as the raw input

the output is coming out as: ('C:\\Fahad\\ACC\\Output\\', 'test.txt')
instead i need it to come out clean as "C:\Fahad\ACC\Output\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.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#4: Jun 11 '09

re: remove whitespace from directory path


The strings must use "\\" or "/" to represent a valid path. Try using os.path.join() or string method join(). Example:
Expand|Select|Wrap|Line Numbers
  1. >>> import os
  2. >>> dir_path = "C:\\Fahad\\ACC\\Output"
  3. >>> accfile = raw_input("Enter file name: ")
  4. >>> print accfile
  5. test.txt
  6. >>> os.path.join(dir_path, accfile)
  7. 'C:\\Fahad\\ACC\\Output\\test.txt'
  8. >>> print os.path.join(dir_path, accfile)
  9. C:\Fahad\ACC\Output\test.txt
  10. >>> dir_path = "C:/Fahad/ACC/Output"
  11. >>> '/'.join([dir_path, accfile])
  12. 'C:/Fahad/ACC/Output/test.txt'
  13. >>> 
Newbie
 
Join Date: Oct 2007
Posts: 28
#5: Jun 11 '09

re: remove whitespace from directory path


Thanks a lot man it worked, you are incredible at this.
bvdet's Avatar
Moderator
 
Join Date: Oct 2006
Location: Nashville, TN
Posts: 1,563
#6: Jun 11 '09

re: remove whitespace from directory path


You are welcome. Keep posting.

-BV
Reply