readlines在创建文件后显示为空

时间:2014-06-13 17:28:45

标签: python readlines

我希望创建一个文件然后读取我刚刚放入的值并将这些值放入列表中。我使用readlines这样做,但是我得到了"列表分配索引超出范围"错误告诉我它没有读取文件。

这是我的代码:

import os

class open_saved_file():

    def __init__(self):
        self.path = os.getcwd()
        self.path_extension = "/Saved_Paths"
        self.file_extension = "/Paths.txt"
        self.paths = []

    def test_path(self):

        test_bool = os.path.exists(self.path+self.path_extension)
#1) path does not exist
        if not test_bool:
            print "The start folder does not exist, let's create it"
            os.makedirs(self.path+self.path_extension)
            print "just made file, now make Paths.txt"
            of = open(self.path+self.path_extension+self.file_extension, 'w+')
            print "Lets add 4 dummy lines of code"
            i = 0
            while i < 4:
                of.write("dummy\n")
                i = i+1
            print "just added four lines of dummy"
            of.close()
            print "lets test"
            self.read_lines_into_list()
            return self.paths
#2) path and file exist
        else:
            print "The start folder did exist, yay!"
            print "lets open it and put its value into paths list"
            self.read_lines_into_list()
            return self.paths

    def read_lines_into_list(self):
        of = open(self.path+self.path_extension+self.file_extension)
        lines = of.readlines()
        self.paths[0] = lines[0][:-1]
        self.paths[1] = lines[1][:-1]
        self.paths[2] = lines[2][:-1]
        self.paths[3] = lines[3][:-1]
        print "lets test if all the values went in"
        j = 0
        while j < 4:
            print self.paths[j]
            i = i+1

3 个答案:

答案 0 :(得分:2)

发生错误不是因为lines为空,而是因为self.paths。而不是:

self.paths[0] = lines[0][:-1]

使用append()

self.paths.append(lines[0][:-1])

您的while循环中也会出现错误,因为当您的循环变量为i = i+1时,您正在执行j。您只需将i替换为j,或者甚至更好地使用for循环:

for path in self.paths:
    print path

答案 1 :(得分:0)

您还可以添加对os.path.isfile的调用以验证文件是否存在。

答案 2 :(得分:0)

以下是代码的修改版本。

import os
class OpenSavedFile():
def __init__(self):
    self.path = os.getcwd()
    self.path_extension = "/Saved_Paths"
    self.file_extension = "/Paths.txt"
    self.paths = []

def test_path(self):
    test_bool = os.path.exists(self.path+self.path_extension)
    if not test_bool:
        print "The start folder does not exist, let's create it"
        os.makedirs(self.path+self.path_extension)
        print "just made file, now make Paths.txt"
        of = open(self.path+self.path_extension+self.file_extension, 'w+')
        print "Lets add 4 dummy lines of code"
        i = 0
        while i < 4:
            of.write("dummy\n")
            i = i+1
        print "just added four lines of dummy"
        of.close()
        print "lets test"
        self.read_lines_into_list()
        return self.paths
    else:
        print "The start folder did exist, yay!"
        print "lets open it and put its value into paths list"
        self.read_lines_into_list()
        return self.paths

def read_lines_into_list(self):
    of = open(self.path+self.path_extension+self.file_extension)
    lines = of.readlines()
    # lines is not empty 
    # remove the comment below and see
    # print lines
    for line in lines:
        self.paths.append(line[:-1]) # paths is an empty list so append the contents
    print "lets test if all the values went in"
    j = 0
    while j < 4:
        print self.paths[j]
        j += 1 # this is not i += 1

op = OpenSavedFile()
op.test_path()