我的文件是“xml.txt”,包含以下内容:
books.xml
news.xml
mix.xml
如果我使用 readline() 函数,它会在所有文件的名称处附加“\n”,这是一个错误,因为我想打开 xml.txt 中包含的文件。我写了这个:
fo = open("xml.tx","r")
for i in range(count.__len__()): #here count is one of may arrays that i'm using
file = fo.readline()
find_root(file) # here find_root is my own created function not displayed here
运行此代码时遇到错误:
IOError: [Errno 2] No such file or directory: 'books.xml\n'
仅删除末尾的换行符:
line = line.rstrip('\n')
原因readline
保留换行符,以便您可以区分空行(有换行符)和文件末尾(空字符串)。
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)