如何在python中的单个列表中添加多个值?

时间:2019-04-04 09:49:02

标签: python

我有两个文件:

  • D:/Users/SPate233/Downloads/NS dashboard/sql_query\Book1.csv
  • D:/Users/SPate233/Downloads/NS dashboard/sql_query\Book2.csv

我想将它们添加到一个列表中-

输出

files = ["D:/Users/SPate233/Downloads/NS dashboard/sql_query\Book1.csv", "D:/Users/SPate233/Downloads/NS dashboard/sql_query\Book2.csv"]

代码-

        path = '/nsmnt/NS_Exec_DSHBD/output/*.csv'
        files = glob.glob(path)
        file_list = []
        for name in files:
            size = os.stat(name).st_size
            if size <= 5:
                print("file %s is empty" %name)
                log.info("file %s is empty" %name)
                file_list = file_list.append(name.split(','))

错误:-

'NoneType' object is not iterable

2 个答案:

答案 0 :(得分:0)

    path = '/nsmnt/NS_Exec_DSHBD/output/*.csv'
    files = glob.glob(path)
    file_list = []
    for name in files:
        size = os.stat(name).st_size
        if size <= 5:
            print("file %s is empty" %name)
            log.info("file %s is empty" %name)
            file_list += name.split(',')

请注意,name.split(',')将生成一个列表,append用于单个值+ =表示lista = lista + listb,换句话说,将name.split的样式添加到file_list

答案 1 :(得分:0)

file_list = [i for i in glob.glob(path) if os.stat(i).st_size <= 5]