ok, so I have to write a program for computer science and I chose to do it in python (obviously) butI'm having trouble getting the program to open the file.

here is just a snippet of the coding in question:

elif choice == 5:
h = open("C:/Users/Dhyan/Documents/" + raw_input("Please enter the customers unique ID again... ") + ".txt")

Although when I run the program it just doesn't open
Any help would be really appreciated.

PS. when It asks me that question I put in a name of a file that does exist
and yes I tried a more simplified version (h = open("C:/Users/Dhyan/Documents/234.txt"))

Recommended Answers

All 9 Replies

Try putting an r before the string to make it a raw text and therefore you wont get suff ups due to escaped characters:

elif choice == 5:
    h = open(r"C:/Users/Dhylan/Documents/"+raw_input()+".txt")

Hope that helps, if not the please help us help you by posting the exact error you get.

It doesn't actually come up with an error. The text file just won't open

Are you sure you're actually executing that statement?

I would have written your code as ff:

elif choice == 5:
	hisin = raw_input("Please enter the customers unique ID again... ")
	fname = "C:/Users/Dhyan/Documents/" + hisin + ".txt"
	h = open(fname)
	if not h:
		print "Unable to open input file %s, choice %d" % (fname, choice)

Fussy-wussy, or good programming practice? You decide (it IS election day after all).

Fussy-wussy, or good programming practice? You decide (it IS election day after all).

most probably good programming practice. I'll admit my structure probably sucks because I've only been doing python for about a week.
and election day means nothing to me as I'm not in the U.S.

You say it doesn't open, but in that case there must be an exception traceback. What is the exception traceback ?

Is there anything in the text file now?

Because if it opens it will normally open in read mode and if there is nothing in the text file then there would be no lines to print to screen.

the text file does have content. when I get it run that bit of code nothing happens and it just goes back to the menu. (could it be because im using vista?)

And you are sure that it is going to that if statement? If not then put a print statement in there with a message so you know when something has entered the if statement.

yes it does enter the statement

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.