Python 创建文件后,readlines显示为空
我希望创建一个文件,然后读取刚才输入的值,并将这些值放入列表中。我使用readlines这样做,但是我得到了“列表分配索引超出范围”错误,它告诉我它没有读取文件 这是我的密码:Python 创建文件后,readlines显示为空,python,readlines,Python,Readlines,我希望创建一个文件,然后读取刚才输入的值,并将这些值放入列表中。我使用readlines这样做,但是我得到了“列表分配索引超出范围”错误,它告诉我它没有读取文件 这是我的密码: import os class open_saved_file(): def __init__(self): self.path = os.getcwd() self.path_extension = "/Saved_Paths" self.file_exten
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
导入操作系统
类打开保存的文件():
定义初始化(自):
self.path=os.getcwd()
self.path_extension=“/Saved_路径”
self.file_扩展名=“/Paths.txt”
self.path=[]
def测试路径(自身):
test\u bool=os.path.exists(self.path+self.path\u扩展)
#1) 路径不存在
如果不是测试布尔:
打印“开始文件夹不存在,让我们创建它”
os.makedirs(self.path+self.path\u扩展)
打印“刚生成文件,现在生成路径.txt”
of=打开(self.path+self.path\u extension+self.file\u扩展名“w+”)
打印“让我们添加4行伪代码”
i=0
而我<4:
of.write(“伪\n”)
i=i+1
打印“刚刚添加了四行虚拟对象”
of.close()
打印“lets test”
self.read\u行\u到\u列表()中
返回自我路径
#2) 路径和文件是否存在
其他:
打印“开始文件夹确实存在,耶!”
打印“让我们打开它并将其值放入路径列表”
self.read\u行\u到\u列表()中
返回自我路径
def将_行_读入_列表(自身):
of=打开(self.path+self.path扩展名+self.file扩展名)
行=of.readlines()
self.path[0]=行[0][:-1]
self.path[1]=行[1][:-1]
self.path[2]=行[2][:-1]
self.path[3]=行[3][:-1]
打印“让我们测试是否所有值都进入”
j=0
当j<4时:
打印自我路径[j]
i=i+1
发生错误不是因为行
为空,而是因为自身路径
为空。而不是:
self.paths[0] = lines[0][:-1]
使用append()
:
您的while
循环中也会出现错误,因为当循环变量为j
时,您正在执行i=i+1
。您可以简单地将i
替换为j
,或者更好地使用for
循环:
for path in self.paths:
print path
您还可以添加对os.path.isfile的调用,以验证该文件是否存在。以下是代码的修改版本
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()
导入操作系统
类OpenSavedFile():
定义初始化(自):
self.path=os.getcwd()
self.path_extension=“/Saved_路径”
self.file_扩展名=“/Paths.txt”
self.path=[]
def测试路径(自身):
test\u bool=os.path.exists(self.path+self.path\u扩展)
如果不是测试布尔:
打印“开始文件夹不存在,让我们创建它”
os.makedirs(self.path+self.path\u扩展)
打印“刚生成文件,现在生成路径.txt”
of=open(self.path+self.path_扩展名+self.file_扩展名'w+'))
打印“让我们添加4行伪代码”
i=0
而我<4:
of.write(“伪\n”)
i=i+1
打印“刚刚添加了四行虚拟对象”
of.close()
打印“lets test”
self.read\u行\u到\u列表()中
返回自我路径
其他:
打印“开始文件夹确实存在,耶!”
打印“让我们打开它并将其值放入路径列表”
self.read\u行\u到\u列表()中
返回自我路径
def将_行_读入_列表(自身):
of=打开(self.path+self.path扩展名+self.file扩展名)
行=of.readlines()
#行不是空的
#删除下面的注释并参阅
#打印行
对于行中的行:
self.paths.append(第[:-1]行]#paths是一个空列表,因此要追加内容
打印“让我们测试是否所有值都进入”
j=0
当j<4时:
打印自我路径[j]
j+=1#这不是i+=1
op=OpenSavedFile()
op.测试路径()
您确认文件确实是在您指定的位置创建的吗?是的,我确认了。它是在错误出现之前创建的,将错误包含在您的帖子中。作者的原始代码不会无限重复列表,因此这是不正确的。当到达i=i+1
时,抛出一个UnboundLocalError
,程序退出。是的,在整个迭代过程中j的值为0,因此将抛出一个UnboundLocalError
。
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()