脚本在计划执行时部分读取文本文件

Script reads text file partially on a scheduled execution

我在 PythonAnyWhere.com 遇到了一个奇怪的问题: 文件在手动执行时由脚本正常读取,但在计划执行时仅部分读取:

脚本

items_list=[]
with open(file) as f: 
    i=1
    for line in f:
        print 'line {0}.: {1}'.format(i, line)
        if i>4:
            temp = line.split()
            single_tuple = temp[0],temp[5]  
            items_list.append(single_tuple)
        i+=1
print items_list

要读取的文件

# Netscape HTTP Cookie File
# http://curl.haxx.se/rfc/cookie_spec.html
# This is a generated file!  Do not edit.

.google.com TRUE    /   FALSE   1507718426  NID  7gVyyZ0xNYH0Rnrvjr
.youtube.com    TRUE    /   FALSE   1512945206  VISITOR_INFO1_LIVE  CgHkhNJriN0

手动执行结果:

line 1.: # Netscape HTTP Cookie File
line 2.: # http://curl.haxx.se/rfc/cookie_spec.html
line 3.: # This is a generated file!  Do not edit.
line 4.: 
line 5.: .google.com TRUE / FALSE   1507718426  NID  Ixa47gVyyZ0xNYH0Rnrvjr
line 6.: .youtube.com   TRUE  /  FALSE  1512945206  VISITOR_INFO1_LIVE      CgHkhNJriN0

 [('.google.com', 'NID', ), ('.youtube.com', 'VISITOR_INFO1_LIVE' )]

导致计划执行。 (日志):

 line 1.: # Netscape HTTP Cookie File
 line 2.: # http://curl.haxx.se/rfc/cookie_spec.html
 line 3.: 
 []
 2017-04-11 11:02:09 -- Completed task, took 1.00 seconds, return code was 1.

可能是什么问题?

这很可能是由于您 运行 脚本与 运行 计划任务时的工作目录不同。尝试使用文件的完整路径,看看是否能解决问题?

https://help.pythonanywhere.com/pages/NoSuchFileOrDirectory