关于这个python脚本的一个问题!

关于这个python脚本的一个问题!,python,Python,当我输入一个文件名,但它没有继续,为什么?如果我输入一个名为company.txt的文件,但它始终显示该文件不存在。为什么?您当前的工作目录不包含company.txt。 设置当前工作目录或使用绝对路径 您可以按如下方式更改工作目录: if __name__=="__main__": fname= raw_input("Please enter your file:") mTrue=1 Salaries='' Salarieslist={} Employe

当我输入一个文件名,但它没有继续,为什么?如果我输入一个名为company.txt的文件,但它始终显示该文件不存在。为什么?

您当前的工作目录不包含
company.txt
。 设置当前工作目录或使用绝对路径

您可以按如下方式更改工作目录:

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

您当前的工作目录不包含
company.txt
。 设置当前工作目录或使用绝对路径

您可以按如下方式更改工作目录:

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

我可以给你们一些提示,帮助你们更好地解决问题

创建一个函数并在main中调用它,例如

import os
os.chdir(new_path)
如果mTrue==1,不要将整个块放在
下:
,而是在出现错误时从函数返回

if __name__=="__main__":
    main()
从不捕获所有异常,而是捕获特定异常,例如IOError

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
否则,捕获所有异常可能捕获语法错误或拼写错误的名称等

打印您得到的异常,可能不总是找不到文件,可能是您没有读取权限或类似的权限


最后,您的问题可能是,文件可能不存在,请尝试输入完整路径。我可以给您一些提示,帮助您更好地解决问题

创建一个函数并在main中调用它,例如

import os
os.chdir(new_path)
如果mTrue==1,不要将整个块放在
下:
,而是在出现错误时从函数返回

if __name__=="__main__":
    main()
从不捕获所有异常,而是捕获特定异常,例如IOError

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
否则,捕获所有异常可能捕获语法错误或拼写错误的名称等

打印您得到的异常,可能不总是找不到文件,可能是您没有读取权限或类似的权限


最后,您的问题可能只是,文件可能不存在,请尝试输入完整路径

,此外,您还应考虑捕获异常对象本身,以便将其字符串表示形式打印为错误消息的一部分:

try:
   f1 = open(fname):
except IOError,e:
  print 'The %s does not exist!'%fname
(您还可以了解有关特定类型的异常对象的更多信息,或许还可以处理
代码中存在某些类型的异常。您甚至可以从解释器中捕获异常以供自己检查,这样您就可以对它们运行
dir()
,以及
type()
在您找到的每个有趣的属性上…等等。

除了更具体地说明要捕获哪些异常外,您还应该考虑捕获异常对象本身,以便可以打印它的字符串表示形式作为错误消息的一部分:

try:
   f1 = open(fname):
except IOError,e:
  print 'The %s does not exist!'%fname
(您还可以了解有关特定类型的异常对象的更多信息,或许还可以处理
代码中存在某些类型的异常。您甚至可以从解释器中捕获异常以供自己检查,这样您就可以对它们运行
dir()
,以及
type()
在您找到的每一个有趣的属性上……等等。

因为它不在工作目录中,至少不在工作目录中。因为它不在工作目录中,至少不在工作目录中。