读取文本文件时,发现读取内容为空,但是确实是有内容的。

if os.path.exists('linkedFile/new_urls.txt'):
    with codecs.open('linkedFile/new_urls.txt','r','utf-8') as f:
        if len(f.read()) !=0:
            res_urls=f.readlines()
            return res_urls

查阅资料后,发现需要使用seek方法把读取指针移到文件开头位置。

if os.path.exists('linkedFile/new_urls.txt'):
    with codecs.open('linkedFile/new_urls.txt','r','utf-8') as f:
        if len(f.read()) !=0:
            f.seek(0) #加入这一行代码
            res_urls=f.readlines()
            return res_urls

更多推荐

踩坑之python读取txt为空白