2

I have a folder structure:

I am using os.walk(path) to get all the files from the "test" folder. I would like to all files except the folder "B" and the files inside it.

test (root-folder)

  • t1.txt
  • t2.txt
  1. A
  • f.txt
  1. B
  • f1.txt
  1. C
  • f4.txt
list1 = ['A', 'C']
result = [os.path.join(dp, f) for dp, dn, filenames in os.walk(path) for f in filenames if os.path.splitext(f)[1] == '.txt']
  for items in result:
     for fname in list1:
       if fname in items.lower():
         result.remove(items)

print(result)

I tried it, but it takes only the A and C. Not the files in main folder? Can you help? Where am i wrong?

Thank you

Abhijit Sarkar
  • 21,927
  • 20
  • 110
  • 219
  • I'm not an expert at list comprehensions (and I will not pretend that I understand yours), but are you checking for ONLY zip-files at the end when you write `os.path.splitext(f)[1] == '.zip'`? It confuses me, since you have no .zip files in the provided example. – dekuShrub Mar 08 '22 at 20:26
  • ah sry, let me change it. –  Mar 08 '22 at 20:27
  • An easier way to get all files recursively is `glob.glob("**/*")` – kwsp Mar 08 '22 at 20:31
  • Does this answer your question? [Python - walk through a huge set of files but in a more efficient manner](https://stackoverflow.com/questions/21718179/python-walk-through-a-huge-set-of-files-but-in-a-more-efficient-manner) – Abhijit Sarkar Mar 08 '22 at 20:43
  • @AbhijitSarkar : thanks, already tried that part and isn't working as expected. –  Mar 08 '22 at 21:08
  • 1
    @PUser what do you mean by path 'B' is dynamic? – jezza_99 Mar 08 '22 at 21:23

2 Answers2

4

Possible solution is to use glob library:

import glob

dir_to_exclude = ['B', 'C']

files = glob.glob('**/*.txt', recursive=True)
files_paths = [_ for _ in files if _.split("\\")[0] not in dir_to_exclude]
files_names = [_.split("\\")[-1] for _ in files if _.split("\\")[0] not in dir_to_exclude]

print(f'List of file names with path: {files_paths}')
print(f'List of file names: {files_names}')
gremur
  • 1,645
  • 2
  • 7
  • 20
2

I think this should work

file_paths = []
forbidden_path = GetForbiddenPath()

for root, dirs, files in os.walk(path):
    for name in files:
        file_path = os.path.join(root, name)
        if forbidden_path in file_path:
            if os.path.splitext(file_path)[1] == '.txt':
                file_paths += [file_path]
dekuShrub
  • 466
  • 4
  • 20