By using this site, you agree to our updated Privacy Policy and our Terms of Use. Manage your Cookies Settings.
443,768 Members | 2,004 Online
Bytes IT Community
+ Ask a Question
Need help? Post your question and get tips & solutions from a community of 443,768 IT Pros & Developers. It's quick & easy.

remove whitespace from directory path

P: 28
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
Jun 11 '09 #1
Share this Question
Share on Google+
5 Replies


bvdet
Expert Mod 2.5K+
P: 2,851
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
Jun 11 '09 #2

P: 28
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.
Jun 11 '09 #3

bvdet
Expert Mod 2.5K+
P: 2,851
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. >>> 
Jun 11 '09 #4

P: 28
Thanks a lot man it worked, you are incredible at this.
Jun 11 '09 #5

bvdet
Expert Mod 2.5K+
P: 2,851
You are welcome. Keep posting.

-BV
Jun 11 '09 #6

Post your reply

Sign in to post your reply or Sign up for a free account.