473,322 Members | 1,562 Online
Bytes | Software Development & Data Engineering Community
Post Job

Home Posts Topics Members FAQ

Join Bytes to post your question to a community of 473,322 software developers and data experts.

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.

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
5 8038
bvdet
2,851 Expert Mod 2GB
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
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
2,851 Expert Mod 2GB
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
Thanks a lot man it worked, you are incredible at this.
Jun 11 '09 #5
bvdet
2,851 Expert Mod 2GB
You are welcome. Keep posting.

-BV
Jun 11 '09 #6

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

Similar topics

9
by: Thomas Mlynarczyk | last post by:
Which is the simplest way to remove all whitespace from a string? Is there a simpler method than a regex replace? Or how can I tell a regex pattern to ignore all whitespace in my subject string?...
2
by: kbass | last post by:
I would like to remove file that are older than 7 days old from a directory. I can do this in shell script rather easy but I would like to integrate this functionality into my Python program. How...
23
by: Stan Cook | last post by:
I was trying to take a list of files in a directory and remove all but the ".dbf" files. I used the following to try to remove the items, but they would not remove. Any help would be greatly...
5
by: Andy Leszczynski | last post by:
How remove directories with the content in the platform independent way? Is the API for that? Thx, A.
19
by: Steve Franks | last post by:
I am using VS.NET 2005 beta 2. When I run my project locally using the default ASP.NET Development Web Server it runs using a root address like this: http://localhost:11243/testsite/ However...
10
by: Brian Gruber | last post by:
Hi, I'm looking for a way to rename a whole directory of files in short order. The files in the directory have different lengths, however all of them end with _xxxx the x's represent a randomly...
15
by: morleyc | last post by:
Hi, i would like to remove a number of characters from my string (\t \r \n which are throughout the string), i know regex can do this but i have no idea how. Any pointers much appreciated. Chris
2
by: Javier | last post by:
Hello all, I'm trying to remove a hidden file (in UNIX) using the Boost::filesystem library. This is what I have: #include <boost/filesystem/operations.hpp> namespace...
4
by: =?Utf-8?B?U3VzaGlTZWFu?= | last post by:
I have some site on asp.net 2.0 and it works with directories - create and remove them with files inside. I never have any problems with create, but a lot of times when I am tring remove directory...
0
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
1
isladogs
by: isladogs | last post by:
The next Access Europe meeting will be on Wednesday 6 Mar 2024 starting at 18:00 UK time (6PM UTC) and finishing at about 19:15 (7.15PM). In this month's session, we are pleased to welcome back...
0
by: Vimpel783 | last post by:
Hello! Guys, I found this code on the Internet, but I need to modify it a little. It works well, the problem is this: Data is sent from only one cell, in this case B5, but it is necessary that data...
0
by: ArrayDB | last post by:
The error message I've encountered is; ERROR:root:Error generating model response: exception: access violation writing 0x0000000000005140, which seems to be indicative of an access violation...
1
by: PapaRatzi | last post by:
Hello, I am teaching myself MS Access forms design and Visual Basic. I've created a table to capture a list of Top 30 singles and forms to capture new entries. The final step is a form (unbound)...
1
by: CloudSolutions | last post by:
Introduction: For many beginners and individual users, requiring a credit card and email registration may pose a barrier when starting to use cloud servers. However, some cloud server providers now...
1
by: Defcon1945 | last post by:
I'm trying to learn Python using Pycharm but import shutil doesn't work
0
by: af34tf | last post by:
Hi Guys, I have a domain whose name is BytesLimited.com, and I want to sell it. Does anyone know about platforms that allow me to list my domain in auction for free. Thank you
0
by: Faith0G | last post by:
I am starting a new it consulting business and it's been a while since I setup a new website. Is wordpress still the best web based software for hosting a 5 page website? The webpages will be...

By using Bytes.com and it's services, you agree to our Privacy Policy and Terms of Use.

To disable or enable advertisements and analytics tracking please visit the manage ads & tracking page.