2014-04-02 73 views
-1
def save_list(todolist, filename): 
    """ writes the todo list to the filename in correct format 

    save_list(todolist, filename) -> list 
    """ 

    fd = open(filename, 'w') #creates file 
    for line in fd: 
     date = as_date_string(line[0]) #to put into correct format 
     chore = line[1] # assigns chore from touple value 
     fd.writelines(text) 
     fd.close() 
    print result 

当我尝试运行这个功能我得到的错误文件无法打开阅读

Traceback (most recent call last): 
    File "<pyshell#0>", line 1, in <module> 
    save_list(load_list('todo.txt'), 'todo.txt') 
    File "C:\Users\Sam\Desktop\CSSE1001\Assignment\assign1.py", line 58, in save_list 
    for line in fd: 
IOError: File not open for reading 

功能应该加载列表和写入列表保存到文件 用于如 save_list(load_list('todo.txt'), 'todo.txt') 应该用相同的信息重写文件

回答

2

由于错误清楚地表明,该文件不能打开阅读。你需要打开它进行读/写:

fd = open(filename, 'r+') 

,我建议你检查出蟒蛇如何read and write files

编辑

此外,作为Dannnno指出,您要关闭的文件,DE环内。您需要将fd.close()移出for循环。

0

看看你的代码。你关闭你的for循环内的文件。你也只写,你想读/写

fd = open(filename, 'r+') #creates file 
for line in fd: 
    date = as_date_string(line[0]) #to put into correct format 
    chore = line[1] # assigns chore from touple value 
    fd.writelines(text) 
fd.close() 

您还没有定义text任何地方,但我不知道它应该是这样,我不能帮你那里