raw_input can't handle pound sign?

J

Jugdish

Hi, I'm having problems getting a pound sign to go through as input
sent to the raw_input() command. I'm running Python 2.5.1 on Windows
XP. Here's my simple little script:

while True:
response = raw_input("Please enter a file name: ")
if os.path.exists(response):
break

Problem is if the filename has a "#" in it, the script interprets that
as the beginning of a comment (not sure why -- isn't raw_input
supposed to treat the user's input as raw text and not do any sort of
evals?)

Any ideas how to get a # to go through? Thanks!
 
S

Steven D'Aprano

Hi, I'm having problems getting a pound sign to go through as input sent
to the raw_input() command. I'm running Python 2.5.1 on Windows XP.
Here's my simple little script:

while True:
response = raw_input("Please enter a file name: ") if
os.path.exists(response):
break

Problem is if the filename has a "#" in it, the script interprets that
as the beginning of a comment (not sure why -- isn't raw_input supposed
to treat the user's input as raw text and not do any sort of evals?)

Yes it is. What makes you think it is being interpreted as a comment?
What results are you getting?

Any ideas how to get a # to go through? Thanks!

Works for me (although I'm not using Windows XP).

Can you execute this line at the interactive interpreter?

print raw_input("Type something with a hash sign: ")


At the prompt, type "test # string" (without the quotes) and show us what
result you get. When I do this, I get the following:

Type something with a hash sign: test # string
test # string
 
J

Jugdish

Ahh ok, tried out your example and it works just fine. Turns out the
actual problem is what I was doing with the input. Elsewhere, I call
urlparse.urlparse() on the filename past in, which splits up the
filename where the # sign is, so that's why it looked to me like the
characters after the # were getting stripped away.

Thanks for your help, in the future I'll try to do a bit more
debugging of my own scripts before bringing my problems here! :)
 

Ask a Question

Want to reply to this thread or ask your own question?

You'll need to choose a username for the site, which only take a couple of moments. After that, you can post your question and our members will help you out.

Ask a Question

Members online

No members online now.

Forum statistics

Threads
473,766
Messages
2,569,569
Members
45,043
Latest member
CannalabsCBDReview

Latest Threads

Top