开发者

a question about this python script!

if __name__=="__main__":
    fname= raw_input("Please enter your file:")
    mTrue=1
    Salaries=''
    Salarieslist={}
    Employeesdept=''
    Employeesdeptlist={}
    try:
        f1=open(fname)
    except:
        mTrue=0
        print 'The %s does not exist!'%fname
    if mTrue==1:
        ss=[]   
        for x in f1.readlines():
            if 'Salaries' in x:
                Salaries=x.strip()
            elif 'Employees' in x:
                Employeesdept=x.strip()
        f1.close()
        if Salaries and Employeesdept:
            Salaries=Salaries.split('-')[1].strip().split(' ')
            for d in Salaries:
                s=d.strip().split(':')
                Salarieslist[s[0]]=s[1]
            Employeesdept=Employeesdept.split('-')[1].strip().split(' ')
            for d in Employeesdept:
                s=d.strip().split(':')
                Employeesdeptlist[s[0]]=s[1]
            print "1) what is the average salary in the company: %s "%Salarieslist['Avg']            
            print "2) what are the maximum and minimum salaries in the company: maximum:%s,minimum:%s "%(Salarieslist['Max'],Salarieslist['Min'])
            print "3) How many employees are there in each department :IT:%s, Development:%s, Administration:%s"%(
                Employeesdeptlist['IT'],Employeesdeptlist['Development'],Employeesdeptlist['Administration'])


        else:
            print 'The %s data is err!'%fname

When I enter a filename, but it didn'开发者_如何学Got continue, why? If I enter a file named company.txt, but it always show the file does not exist. why?


I can give you some hints which can help you to resolve problem better

Create a function and call it in main e.g.

if __name__=="__main__":
    main()

Don't put whole block under if mTrue==1: instead just return from function on error e.g.

def main():
    fname= raw_input("Please enter your file:")
    try:
        f1=open(fname)
    except:
        print 'The %s does not exist!'%fname
        return

    ... # main code here

Never catch all exceptions , instead catch specific exception e.g. IOError

try:
   f1 = open(fname):
except IOError,e:
  print 'The %s does not exist!'%fname

otherwise catching all exception may catch syntax error or mis-spelled names etc

Print the exception you are getting, it may not always be file not found, may be you don't have read permission or something like that

and finally your problem could be just that, file may not exist, try to input full path


Your current working directory does not contain company.txt. Either set your current working directory or use an absolute path.

You can change the working directory like so:

import os
os.chdir(new_path)


In addition to be more specific about which exceptions you want to catch you should considered capturing the exception object itself so you can print a string representation of it as part of your error message:

try:
    f1 = open(fname, 'r')
except IOError, e:
    print >> sys.stderr, "Some error occurred while trying to open %s" % fname
    print >> sys.stderr, e

(You can also learn more about specific types of Exception objects and perhaps handle some sorts of exceptions in your code. You can even capture Exceptions for your own inspection from within the interpreter so you can run dir() on them, and type() on each of the interesting attributes you find ... and so on.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜