Vince V Vanhoyte
Vince V Vanhoyte

Reputation: 33

Converting List of file in directory to list so that it can be itterated-python

So I want to create make a list of a directory so that I can iterate it. The goal is to be able to have a function look at the list and run the function on every line. This is what I did:

import re
import os
import json

#this is what should search a list all files in directory
def list_of_files():
    path = '/mnt/c/Users/deni/desktop/chatette/'
    files = os.listdir(path)
    for f in files:
        print(f)
        return(f)


#this is where the file is loaded
def load_file(filename):
    loadfile = open("filename, "r")
    replace_name = os.path.basename(loadfile.name)
    name_of_file = os.path.splitext(replace_name)[0]

    if loadfile.mode == 'r':
        contents = loadfile.read()
        remove_dashes = re.sub("-","", contents)
        remove_hashes =re.sub("##", "", remove_dashes)
        remove_intent =re.sub("intent", "", remove_hashes)
        remove_colan =re.sub(":", "", remove_intent)
        remove_generic =re.sub("Generic", "", remove_colan)
        remove_critical =re.sub("critical", "", remove_generic)
        remove_line_one=re.sub("<! Generated using Chatette v1.6.2 >", name_of_file, remove_critical)
        edited_contents = remove_line_one   
        #print(edited_contents)
        return(edited_contents)

#this is suppose to iterate the file and run the function for each file listed
listoffile = list_of_files()

for txt in listoffile:
    for i in list_of_files():
        if i.endswith(".xlsx"):
            load_file(txt)

How ever this is the response I am getting

Traceback (most recent call last):
  File "generate.py", line 68, in <module>
    for txt in listoffile:
TypeError: 'NoneType' object is not iterable

can you help me with this?

Upvotes: 0

Views: 1881

Answers (3)

Hassan
Hassan

Reputation: 626

There are two options I can think of:

  • return a list of files
  • use a generator to return the files one at a time

I'll start with the first one since it's easier to implement:

def list_of_files():
    path = '/mnt/c/Users/deni/desktop/chatette/'
    return os.listdir(path)

#...
listoffile = list_of_files()

for txt in listoffile:
    if i.endswith(".xlsx"):
        load_file(txt)

The second one is more optimal in terms of memory usage because it doesn't pre-allocate a list of files before returning :

def list_of_files():
    path = '/mnt/c/Users/deni/desktop/chatette/'
    files = os.listdir(path)
    for f in files:
        print(f)
        yield f
#...
listoffile = list_of_files()

for txt in listoffile:
    if i.endswith(".xlsx"):
        load_file(txt)

Upvotes: 1

Nick Humrich
Nick Humrich

Reputation: 15755

Looking at your stack trace:

Traceback (most recent call last):
  File "generate.py", line 68, in <module>
    for txt in listoffile:
TypeError: 'NoneType' object is not iterable

We see that the issue is for txt in listoffile: and it is saying that listoffile is None

This means that list_of_files() is returning None. My best guess is that you have no files in the directory you posted: '/mnt/c/Users/deni/desktop/chatette/'

You also probably want list_of_files() to return files instead of the individual file inside of the loop.

Upvotes: 0

Goldwave
Goldwave

Reputation: 599

In the for loop in your "list_of_files" function, you are returning after iterating through only 1 path. Then you print and return that path.

You need to return "files" and not "f", and after the loop finishes.

def list_of_files():
    path = '/mnt/c/Users/deni/desktop/chatette/'
    files = os.listdir(path)
    for f in files:
        print(f)
        return(f)

So it needs to be

def list_of_files():
    path = '/mnt/c/Users/deni/desktop/chatette/'
    files = os.listdir(path)
    for f in files:
        print(f)
    return(files)

Upvotes: 0

Related Questions