3

I want to exclude the directory 'dir3_txt' so that I can only capture the files('.txt') from other directory . I tried to exclude directory like below but not able to figure it out how to get all the files having .txt as ext other that having it in dir3_txt using below:

for root, dirs, files in os.walk('.'):
   print (root)
   dirs[:] = [d for d in dirs if not d.startswith('dir3')]
   for file in files:
        print (os.path.join(root, file))

I am thinking of glob (got below from stack itself) but not sure how to tweak glob to use it.

for file in os.walk('.'):
   for txt in glob(os.path.join(files[0], '*.txt')):
       print(txt)

I went through Excluding directories in os.walk but the solution provided is not helping me, also it only tells about skipping directory that also is not helpful as I need to get files from other directories , better if we can do it with glob only?

eyllanesc
  • 235,170
  • 19
  • 170
  • 241
steveJ
  • 2,171
  • 3
  • 11
  • 16
  • 1
    what your dir tree looks like? – georgexsh Aug 22 '18 at 05:04
  • @georgexsh In current working directory , lets say `/home/users/steve/Desktop` , I have multiple separate folders like `dir1 , dir2, dir3_txt, dir4` and inside `dir3_txt` there can be subfolders having txt files as well. Script is placed in `/home/users/steve/Desktop` – steveJ Aug 22 '18 at 05:09
  • Please read [Under what circumstances may I add “urgent” or other similar phrases to my question, in order to obtain faster answers?](//meta.stackoverflow.com/q/326569) - the summary is that this is not an ideal way to address volunteers, and is probably counterproductive to obtaining answers. Please refrain from adding this to your questions. – halfer Aug 22 '18 at 16:33

2 Answers2

2

A simple solution would just to do a string comparison against the directory-paths and files returned by os.walk:

for root, dirs, files in os.walk('.'):
   if "/dir3_txt/" not in root:
       for file in files:
            if file.endswith(".txt"):
                print (os.path.join(root, file))
maxymoo
  • 35,286
  • 11
  • 92
  • 119
  • But how to include `glob` here to get all the files `.txt` from other directories and its subdirectories. I tried to put `for txt in glob(os.path.join(files[0], '*.txt')):` under the second for loop but its not working to list .txt – steveJ Aug 22 '18 at 05:14
  • do a string comparison against the file (see my edit) – maxymoo Aug 22 '18 at 05:16
  • you could use `fnmatch` for filename matching. – georgexsh Aug 22 '18 at 06:43
1
for root, dirs, files in os.walk('.'):
   print (root)
   dirs[:]= [d for d in dirs if d[:4]!='dir3']
   for file in files:
      if file[-4:]=='.txt':
        print (os.path.join(root, file))

I dont have any system with me now to test this , so if any problems please comment.

Edit:

Now it only detects '.txt' files.

Yashik
  • 391
  • 5
  • 17