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

时间:2017-04-11 12:58:31

标签: python pythonanywhere file-read

我在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.

可能有什么问题?

1 个答案:

答案 0 :(得分:3)

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

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