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

Problems with \t and \b characters

P: 17
hi!

I have a string as "C:\local\test\new"
Let us say str="C:\local\test\new"

I want to extract the part \test\new and print it on the console as such.
i.e is my desired output is:
\test\local (which is to be printed)

I am facing the following problem:
when i am using the function
print str.split("C:\local")[1]

I am getting the following output:
est
ew

(This is because it is considering \t as tab and \n as new line and printing the output accordingly)
HOW TO AVOID THIS??
i.e to get the output as \test\new

PLEASE help me out

Regards,
learnerofpython
Dec 29 '06 #1
Share this Question
Share on Google+
5 Replies


bartonc
Expert 5K+
P: 6,596
hi!

I have a string as "C:\local\test\new"
Let us say str="C:\local\test\new"

I want to extract the part \test\new and print it on the console as such.
i.e is my desired output is:
\test\local (which is to be printed)

I am facing the following problem:
when i am using the function
print str.split("C:\local")[1]

I am getting the following output:
est
ew

(This is because it is considering \t as tab and \n as new line and printing the output accordingly)
HOW TO AVOID THIS??
i.e to get the output as \test\new

PLEASE help me out

Regards,
learnerofpython
Use raw strings for path names.
str = r"C:\local\test\new"
Dec 29 '06 #2

bvdet
Expert Mod 2.5K+
P: 2,851
hi!

I have a string as "C:\local\test\new"
Let us say str="C:\local\test\new"

I want to extract the part \test\new and print it on the console as such.
i.e is my desired output is:
\test\local (which is to be printed)

I am facing the following problem:
when i am using the function
print str.split("C:\local")[1]

I am getting the following output:
est
ew

(This is because it is considering \t as tab and \n as new line and printing the output accordingly)
HOW TO AVOID THIS??
i.e to get the output as \test\new

PLEASE help me out

Regards,
learnerofpython
You should not use 'str' for a variable name since str() is a built-in function. Following are some inelegant options:
Expand|Select|Wrap|Line Numbers
  1. >>> import os
  2. >>> s = "C:\local\test\new"
  3. >>> os.path.join(os.path.basename(os.path.dirname(s)),os.path.basename(s))
  4. 'local\test\new'
  5. >>> s = r"C:\local\test\new"
  6. >>> os.path.join(os.path.basename(os.path.dirname(s)),os.path.basename(s))
  7. 'test\\new'
  8. >>> s = "C:\\local\\test\\new"
  9. >>> os.path.join(os.path.basename(os.path.dirname(s)),os.path.basename(s))
  10. 'test\\new'
  11. >>> s = "C:/local/test/new"
  12. >>> os.path.join(os.path.basename(os.path.dirname(s)),os.path.basename(s))
  13. 'test\\new'
Dec 29 '06 #3

bartonc
Expert 5K+
P: 6,596
You should not use 'str' for a variable name since str() is a built-in function. Following are some inelegant options:
Expand|Select|Wrap|Line Numbers
  1. >>> import os
  2. >>> s = "C:\local\test\new"
  3. >>> os.path.join(os.path.basename(os.path.dirname(s)),os.path.basename(s))
  4. 'local\test\new'
  5. >>> s = r"C:\local\test\new"
  6. >>> os.path.join(os.path.basename(os.path.dirname(s)),os.path.basename(s))
  7. 'test\\new'
  8. >>> s = "C:\\local\\test\\new"
  9. >>> os.path.join(os.path.basename(os.path.dirname(s)),os.path.basename(s))
  10. 'test\\new'
  11. >>> s = "C:/local/test/new"
  12. >>> os.path.join(os.path.basename(os.path.dirname(s)),os.path.basename(s))
  13. 'test\\new'
Good catch on the "str" assignment! The funny thing was when I was replying last night, at first I thought both back slash posts were from the same person. Then I realized differently and fixed one of my posts, but missed the other. Teamwork succeeds where indivual effort falls short! Thanks, Bruce.
Dec 30 '06 #4

bartonc
Expert 5K+
P: 6,596
Good catch on the "str" assignment! The funny thing was when I was replying last night, at first I thought both back slash posts were from the same person. Then I realized differently and fixed one of my posts, but missed the other. Teamwork succeeds where indivual effort falls short! Thanks, Bruce.
This is kind of interesting:

>>> str = "hello"
>>> str(1)
File "<console>", line 1, in ?
''' exceptions.TypeError : 'str' object is not callable '''
>>> del str
>>> str(1)
'1'

You can reassign to a built-in name temporarily. Could come in handy some time...
Dec 30 '06 #5

bartonc
Expert 5K+
P: 6,596
Use raw strings for path names.
str = r"C:\local\test\new"
It turns out that unicode strings also work here:
aPathStr = u"C:\local\test\new"
Jan 16 '07 #6

Post your reply

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