Question

I have a folder of several thousand files. I need to match the filename of each file with a column from another array.

import os

filenames = []
for files in os.walk("Directory"):
    filenames.append(files)

I now have a tuple of every filename in the directory. What I want now is to splice each element of the tuple, and add that to a new array. My code looks similar to this:

files = []
for i in filenames:
    files.append(i[2:7])

I get the error, "AttributeError: 'tuple' object has no attribute 'append'." After searching around here, I tried other commands like join. I tried converting filenames to a list.

Even when I say something like:

filenames[0]

expecting to get only the first string result, it returns the entire filenames array. Is there any way around this?

Thanks for your help.

Was it helpful?

Solution

os.walk returns a 3 tuple: root, dirs, files

Documentation here

You could use list comprehensions to achieve the split based on filenames.

filenames = reduce(lambda x,y: x+y, [files for root, dirs, files in os.walk('.')])
files = [name[2:7] for name in filenames]

Here are the explanations: Lambda functions are anonymous functions. You do not return anything. The definition contains expressions which are returned. This is powerful in processing dynamically generated data.

reduce() a.k.a "worker function" accepts two arguments. The function is called with first two elements of the list, then with the result of these, the third element, and so on. The return value is a single list

There are loads of documentation around this online.

OTHER TIPS

I actually needed to include the path in the filenames and found this useful using list expressions. Since this helped me, I wanted to add the solution that gives a list with the full path:

onlyfiles = reduce(lambda x,y : x+y, [map(lambda x: root + "/" + x, files) for root, dirs, files in os.walk(mypath)])
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top