将文件导入spyder时出错(正确目录)

时间:2016-06-01 04:40:09

标签: python csv pandas anaconda spyder

这是我的代码

SELECT  Distinct
        fa.new_sitename AS Office,
        fs.new_subprogramname As Subprogram,
        Fsrv.name AS Servicetype,
    Count(fa.new_clientidname) As Total#OfClients,
    count(fa1.new_clientidname) as "Total # of Clients receiving FV within 5 days" 
FROM FirstServiceAppointment fs left outer join Firstappointment  fa
on   fs.new_appointmentlookup = fa.new_appointmentid
left outer join Firstappointment  fa1
on   fs.new_appointmentlookup = fa1.new_appointmentid
AND DATEDIFF(DAY,cast(fa.createdon AS Date),cast(fs.scheduledstart AS Date)) >=5   
join FirstService Fsrv
on fa.new_service=Fsrv.serviceid or fa1.new_service=Fsrv.serviceid
Where 
fs.new_visittypename like 'first'
                    AND fs.statuscodename like 'done'
                    AND fa.createdon BETWEEN '05/18/2016' AND '05/30/2016' 
            AND fa1.createdon BETWEEN '05/18/2016' AND '05/30/2016' 
group by fa.new_sitename, fs.new_subprogramname,Fsrv.name

我已经将csv文件放在工作目录中,但我仍然遇到

  

OSError:文件b' all-ages.csv'不存在

但是如果我在控制台中输入每一行而不是脚本,它有时会起作用。

1 个答案:

答案 0 :(得分:1)

您最好提供绝对文件路径。 Python使用当前工作目录,该目录取决于您调用/运行python脚本的位置。

即使您将python脚本和csv文件“all-ages.csv”放在同一目录下,当前工作目录也可能不同。

例如:

/folder1/folder2/myscript.py
/folder1/folder2/all-ages.csv

如果你在目录文件夹2下运行python myscript.py,它可以找到all-ages.csv,但如果你在folder1下调用python folder2/myscript.py,当前的工作目录是folder1,它找不到{{1 }}