2

For this first part, prompt the user for the name of the file containing the keywords, check to make sure that the file exists and if does not exist, my program should generate an exception and exit. Have I done this part right?

done = False
    while not done:
        try:
            keywords = input("Enter file named keywords.txt: ")
            infile = open(keywords, "r")
            done = True
        except IOError :
            print("Error: file not found.")
            exit()
        try:
            tweets = input("Enter file named tweets.txt: ")
            infile = open(tweets, "r")
            done = True
        except IOError :
            print("Error: file not found.")
            exit() #Should exit if doesnt exist? Doesnt say in assignment

Ignore the #statement lol

user
  • 5,370
  • 8
  • 47
  • 75
  • 1
    Why don't you run it and see for yourself? – sobek Nov 10 '16 at 22:17
  • 3
    Why do you use while loop? On the first iteration you either set `done=true` or `exit()`. In both situations, you stop loop. – Yevhen Kuzmovych Nov 10 '16 at 22:19
  • @EvgenyKuzmovich I thought that "if does not exist your program should generate an exception and exit." that I should exit the code if it doesn't exit – HelloWorld4382 Nov 10 '16 at 22:20
  • 1
    You probably should, but there's no reason at all to use the while loop. You might just as well exit after you've successfully opened a file. Btw. if you open a file and never close it, you create a memory leak. – sobek Nov 10 '16 at 22:22
  • @HelloWorld4382 This part is correct. But your while loop is useless. – Yevhen Kuzmovych Nov 10 '16 at 22:24
  • @HelloWorld4382 Your code will always exit the loop on the the first iteration, so the loop isn't doing anything at all. – Carcigenicate Nov 10 '16 at 22:25
  • @EvgenyKuzmovich Ok thanks! Ill just remove the while loop then! – HelloWorld4382 Nov 10 '16 at 22:25
  • Thanks!! I see what you guys mean, It was useless. I think I just added it because I searched it up and someone else's code had it. Thanks! – HelloWorld4382 Nov 10 '16 at 22:27
  • @HelloWorld4382 and btw you should close your file if you successfully opened it. – Yevhen Kuzmovych Nov 10 '16 at 22:28
  • @EvgenyKuzmovich but the next part I have to read the files, should I still close them? – HelloWorld4382 Nov 10 '16 at 22:31
  • No. Use `with open ('filename') as file:` and then call a function that works with the contents. – sobek Nov 10 '16 at 22:32
  • True! Thanks a lot! – HelloWorld4382 Nov 10 '16 at 22:34
  • You need to read that program specification you've given in the question more closely. Why is the first `input` prompt `"Enter file named keywords.txt: "`? Surely it should be more like `"Enter the name of the keywords file: "`. Also, the program specification only asks you to check that the keywords file _exists_ it doesn't say anything about _opening_ it. And what's all the `tweets` stuff for? The program specification says nothing about that! – PM 2Ring Nov 10 '16 at 22:37
  • @PM2Ring you're right about the reading part... I thought to check if a file exists you need to read it. Thats just how it shows it in our chapter slides. Could be wrong. How would I check if it exist without reading? and the tweets part is because the second part is : 3. Your program should then prompt the user for the name of the file with tweets; again, your program should generate an exception if the file does not exist. – HelloWorld4382 Nov 10 '16 at 22:41
  • How to check if a file exists: http://stackoverflow.com/questions/82831/how-do-i-check-whether-a-file-exists-using-python – sobek Nov 10 '16 at 22:44
  • @sobek all of them open the file... Im not interested in the os function though like import os #Your path here e.g. "C:\Program Files\text.txt" if os.path.exists("C:\..."): print "File found!" – HelloWorld4382 Nov 10 '16 at 22:50
  • Well, then don't ask for it... – sobek Nov 10 '16 at 22:53
  • @sobek true thanks! – HelloWorld4382 Nov 10 '16 at 22:54
  • @HelloWorld4382: When asking for help on SO it's a Good Idea to make sure that the program description you put in the question matches what you expect your code to do. Otherwise you may get incorrect advice. Sure, you can test if a file exists by attempting to open it. And that's a perfectly sensible thing to do if you intend to read from the file (or write to it). OTOH, if you _just_ need to test if a file exists the `os.path.exists` is much more efficient. – PM 2Ring Nov 10 '16 at 22:56
  • @PM2Ring Okay! I Understand what you mean.. Sorry for the confusion. I just knew os.path.exists wasnt what I needed because we haven't been taught that. I need to check if the file exists because my next step is to " process the file of tweets by reading the file of tweets line by line as text and break it apart" which I am attempting now – HelloWorld4382 Nov 10 '16 at 23:01

1 Answers1

1

Ok. I think it should look like:

try:
    file_path = input("Enter file named keywords.txt: ") # don't call it keywords, cause it is not
    with open(file_path, "r") as infile: #this will open a file and close it when needed 
        #do what you need to do with your file
except IOError : # it will catch exception if file not found
    print("Error: file not found.")
    exit()
Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48