'NoneType'对象不可迭代python3

时间:2019-03-04 18:57:30

标签: ubuntu python-3.7 import-from-csv

嗨,我一直在研究某些python脚本,而我的问题是在服务器上启动该脚本时。我已经制作了一个名为CSVmanipulation的模块,其中有两个功能,一个用于读取.csv,另一个用于写入.csv文件。我的主脚本看起来像这样

import time
import scrape
import re
from urlload import load_urls
from setmanipulation import addToSet
from CSVmanipulation import Reader, Writer

USERSURLS = Reader("users.csv")

for newurl in USERSURLS:
    "do something"

,我的CSV操作脚本是:

import csv

def Reader(nameOfAFile):
        list = []
        if(os.path.isfile(nameOfAFile) == True):
                with open(nameOfAFile) as fajl:
                        reader = csv.reader(fajl)
                        list = [r for r in reader]
                        fajl.close()
                return list
        else:
                print("file doesnt exist")

在我的机器上,它可以正常工作,可以从.csv文件读取,读取的所有内容都存储在列表中,但是当我在服务器上尝试此操作时,我得到以下输出:

file doesnt exist
'NoneType' object is not iterable

文件存在于本地和服务器上,并且其中包含内容。 我已经摸摸了百万次,并访问了每个站点,但是我不知道到底是怎么了。任何帮助,我将不胜感激。

2 个答案:

答案 0 :(得分:0)

因此,基本上,您的Reader方法找不到文件,并且返回NONE。由于USERURLS现在不存在,因此您不能在其上使用for循环。

USERURLS = Reader("users.csv")之前

尝试print(os.getcwd())

这将打印程序的当前工作目录,并查看该目录是否是您的“ users.csv”文件。

答案 1 :(得分:0)

我的代码有点返工: main.py:

import time
import scrape
import re
from urlload import load_urls, display
from setmanipulation import addToSet
from CSVmanipulation import Reader, Writer

USERSURLS = []
Reader("users.csv", USERSURLS)

for newurl in USERSURLS:
   "do something"

我的CSV操作如下:

import os
import csv

def Reader(nameOfAFile, list):
        statment = os.path.exists(nameOfAFile)
        print(os.getcwd())
        if(statment == True):
                with open(nameOfAFile) as fajl:
                        reader = csv.reader(fajl)
                        list.extend(r for r in reader)
                        fajl.close()
        else:
                print("file doesnt exist")

        return

我只是在请求中将USERSURL设为空列表USERSURL = [],并且由于list是可变对象,所以我只是通过方法发送它,因此所有更改都将生效。

顺便说一句,谢谢您的帮助