0

I am trying to check if 3 files exist in a directory in a recursive way. If one (or all of the files) does not exist, it will write a file in the respective directory.

I found some posts that explian how to do it, as for example here How to recursively go through all subdirectories and read files?

However, the files I want to write should only be written in certain directories. Imagine a diretory structure with three levels. And I want to check and write the files only in the third level.

I tried the following code:

name1 = 'Info_1_250.csv'
name2 = 'Info_1_500.csv'
name3 = 'Info_1_1000.csv'

path_name = '/Users/user/Desktop/test/'

for root, _, files in os.walk(path_name):

        if name1 not in files:

            write_file(os.path.join(root, name1))

        elif name2 not in files:

            write_file(os.path.join(root, name2))

        elif name3 not in files:

            write_file(os.path.join(root, name1))

write_file is a function that I wrote that will write the file.

The problem is that this code write the file "name1" in all directories if it does not exist. It even writes in the top level and 2nd level directories (which I don't want). However in the top level and 2nd level, it only writes the file "name1". The other two files, even though it does exist, it is not written.

Finally, if the three files are missing from a specific directory in level3, all three files should be written. I only would like files to be written in level3.

In other words, I want to turn the following tree:

/a
/a/b
/a/b/c
/a/b/d
/a/e
/a/e/f
/a/e/f/Info_1_250.csv
/a/e/g
/a/e/g/Info_1_500.csv
/h
/h/i
/h/i/j
/h/i/j/Info_1_500.csv
/h/i/j/Info_1_1000.csv

Into the following:

/a
/a/b
/a/b/c
/a/b/c/Info_1_250.csv
/a/b/c/Info_1_500.csv
/a/b/c/Info_1_1000.csv
/a/b/d
/a/b/d/Info_1_250.csv
/a/b/d/Info_1_500.csv
/a/b/d/Info_1_1000.csv
/a/e
/a/e/f
/a/e/f/Info_1_250.csv
/a/e/f/Info_1_500.csv
/a/e/f/Info_1_1000.csv
/a/e/g
/a/e/g/Info_1_250.csv
/a/e/g/Info_1_500.csv
/a/e/g/Info_1_1000.csv
/h
/h/i
/h/i/j/Info_1_250.csv
/h/i/j/Info_1_500.csv
/h/i/j/Info_1_1000.csv

As you can see, for every third-level directory in the tree, the files Info_1_250.csv, Info_1_500.csv and Info_1_1000.csv must be created when missing. The other directories must be left untouched.

Community
  • 1
  • 1
Fabs
  • 149
  • 2
  • 11
  • Dont you want something like: "if (name1 not in files) or (name2 not in files) or (name3 not in files): # code to write the missing file" ? – kist Aug 25 '15 at 17:11
  • I will try to post a picture of the directory file structure that I am trying to work with. Thanks. – Fabs Aug 25 '15 at 17:13
  • Instead of a screenshot, please post the output of `find` before and after. – Andrea Corbellini Aug 25 '15 at 17:15
  • Sorry, but what do you mean by "output of find"? The code writes the files. It just not write "only" where I want it. – Fabs Aug 25 '15 at 17:18
  • 2
    If you just want the *third level* why are you starting at the first? – Padraic Cunningham Aug 25 '15 at 17:27
  • Because I will have to do that for too many directories. If it were just one that contained the 500 directories it would be fine. But I have too many. So I thought there could be a way of recursively checking all the directories and writing in all 3rd levels. – Fabs Aug 25 '15 at 17:34
  • Not sure I understand, if you don't care about those directories why walk them? Also how do you distinguish what is the third level if not by name? – Padraic Cunningham Aug 25 '15 at 17:37

1 Answers1

2

You can try using the glob module to access all third level dirs then check if the files exist in each of them.

import glob 
import os   

names = ['Info_1_250.csv', 'Info_1_500.csv', 'Info_1_1000.csv']

path_name = '/Users/user/Desktop/test/'
third_level_path = os.path.join(path_name, '*', '*')
dirs = [d for d in glob.glob(third_level_path) if os.path.isdir(d)]

for d in dirs:
    for name in names:
        f = os.path.join(d, name)
        if not os.path.isfile(f):
            write_file(f)
Ben
  • 380
  • 1
  • 9
  • @PadraicCunningham glob in itself is not recursive. But when used as `a/*/*` it will give all results from third level that is `a/b/c`, `a/b/d`, ... – shanmuga Aug 25 '15 at 17:57
  • That is true that it is not recursive, but I think it should accomplish the task without recursion. It's unclear whether recursion is a requirement or just the first thing that came to mind. Fabs? – Ben Aug 25 '15 at 18:02
  • I combine part of the code above (the glob bit). So I have the path names to all the directories I am iterested in investigating using the os.walk above. It works! (and yes, recursion was the first think that came to my mind) Thanks!! – Fabs Aug 25 '15 at 18:15