1

When I run this code it prompts for the file name, and regardles whether I enter the path to the file, or drag and drop, it will run the except part of the code 'File cannot be opened' but it will never run fuse = open(fname) or the rest of the program.

Here is the complete program:

fname = raw_input('Enter file name: ')
try:
    fuse = open(fname)
except:
    print 'File cannot be opened'
    raw_input('Press enter to quit')
count = 0
total = 0
for line in fuse:
    if line.notstartswith('X-DSPAM-Confidence:'): continue
    elif line.startswith('X-DSPAM-Confidence:'):
    count = count + 1
    vpos = line.find(' ')
    addv = line[vpos:]
    addv = float(addv)
    total = total + addv

print total/count

Any ideas of what could be wrong?

When I run it without the try and except from the command line to see the error, this is the message I get:

Enter file name: "C:\Users\Gonzalez Cocoma\Documents\Python\Programs\mbox-short.txt" Traceback (most recent call last): File "C:\Users\Gonzalez Cocoma\Documents\Python\Programs\Spamaverage.py", line 2, in fuse = open(fname) IOError: [Errno 22] invalid mode ('r') or filename: '"C:\Users\Gonzalez Cocoma\Documents\Python\Programs\mbox-short.txt"'

9
  • what exactly are you entering the file name as ? and is that file name located in your current working directory? Commented Oct 3, 2015 at 18:38
  • 1
    Maybe you could try to remove the try/except block to see what error is thrown. Commented Oct 3, 2015 at 18:39
  • 3
    No, since you've decided to swallow the exception with a catch-all handler. Commented Oct 3, 2015 at 18:39
  • Anything could be going wrong, but no-one will ever know what it is, because you've done a bare except and haven't reported what the error is. Commented Oct 3, 2015 at 18:40
  • Put except IOError as e: print e instead. Commented Oct 3, 2015 at 18:40

1 Answer 1

1

fname isn't a valid file.

From the prompt:

>>> import os
>>> fname = 'invalid-filename.txt'
>>> os.path.isfile(fname)
False

If you try and open it you will get an exception:

>>> open(fname)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
IOError: [Errno 2] No such file or directory: 'invalid-filename.txt'

You can catch the exception, and print its value:

>>> try:
...     open(fname):
... except IOError as error:
...     print e
[Errno 2] No such file or directory: 'invalid-filename.txt'

The reason the filename wasn't valid is that you'd included quotes.

The error message shows a string '"path/to/file.txt"'. It should show 'path/to/file.txt':

IOError: [Errno 22] invalid mode ('r') or filename:
    '"C:\Users\Gonzalez Cocoma\Documents\Python\Programs\mbox-short.txt"'

Should be:

IOError: [Errno 22] invalid mode ('r') or filename:
    'C:\Users\Gonzalez Cocoma\Documents\Python\Programs\mbox-short.txt'
Sign up to request clarification or add additional context in comments.

6 Comments

I am studying in coursera and reading the book, python for informatics, and have not yet gotten to the lesson where I would learn what "import os" means, please bear with me as I am a real newbie.... as for the fname, should it not be a valid file name after the user inputs the path to the file and it gets assigned to the variable fname??
The filename you gave was "C:\Users\Gonzalez Cocoma\Documents\Python\Programs\mbox-short.txt" including the quotes. You don't need the quotes when typing in the text at the prompt. The representation of the string includes quotes to show you where it begins and ends, but they're not actually part of the string.
I found a working example of what I am doing while browsing the web, and came to the conclusion that the problem is either my file or its location, since even when using the working example, the error remains as follows: line 2, in fuse = open(fname) IOError: [Errno 22] invalid mode ('r') or filename
After removing the " marks, the error changed to : Traceback (most recent call last): File "C:\Python27\Spamaverage.py", line 6, in <module> if line.notstartswith('X-DSPAM-Confidence:'): continue AttributeError: 'str' object has no attribute 'notstartswith'
Well, that's your next problem. Should be if not line.startswith.
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.