-2

Need to append something to the following code:

for subdir, dirs, files in os.walk(rootdir):
    for file in files:
        if file.endswith(".json"):
            json_files.append(os.path.join(subdir, file))
        if file.endswith(".list"):
            table_files.append(os.path.join(subdir, file))

In psuedocode, need to add an if statement like:

if a sub-directory does not contain a file that ends in ".list", 
table_files.append('')

Have tried searching but can't seem to find something that works exactly

  • 2
    Isn't is the same as to check whether `len(table_files)` is equal to `0`? – dcg Feb 24 '20 at 21:06
  • @mkrieger No, not exactly. I need to check whether or not a file with a certain extension exists within a subdirectory – awdoibbbbbbb2 Feb 24 '20 at 21:07
  • @dcg no, I need to add an empty string to the ```table_files``` list if a certain subdirectory does not contain a ```.list``` file – awdoibbbbbbb2 Feb 24 '20 at 21:08
  • Yes, the linked question shows how to find all files with a given extension. In your case you only need to check if this is equal to *no files*. – mkrieger1 Feb 24 '20 at 21:09
  • But you already seem to have done it, I don't quite understand what exactly the remaining problem is. – mkrieger1 Feb 24 '20 at 21:11
  • I'm not checking whether or not a file has a specified extension. I'm checking whether or not a subdirectory contains a file with that extension. Also confused how to tie that into my for loop – awdoibbbbbbb2 Feb 24 '20 at 21:15

1 Answers1

0

You can solve this problem by creating a flag and call it list_found:

for subdir, dirs, files in os.walk(rootdir):
    list_found = False
    for file in files:
        if file.endswith(".json"):
            json_files.append(os.path.join(subdir, file))
        if file.endswith(".list"):
            table_files.append(os.path.join(subdir, file))
            list_found = True

    if not list_found:
        table_files.append('')

Update

Instead of appending an empty string, you can create an empty file (or a file with any contents you want a append that file instead:

import tempfile

# Create an empty file
with tempfile.NamedTemporaryFile(delete=False) as default_file:
    pass
    # or write something to it if you want, such as:
    # default_file.write("Something\n")

for subdir, dirs, files in os.walk(rootdir):
    list_found = False
    for file in files:
        if file.endswith(".json"):
            json_files.append(os.path.join(subdir, file))
        if file.endswith(".list"):
            table_files.append(os.path.join(subdir, file))
            list_found = True

    if not list_found:
        table_files.append(default_file.name)

# After done, delete the file
Hai Vu
  • 37,849
  • 11
  • 66
  • 93
  • thanks for the solution. Quick question - instead of appending an empty string, would it be possible to append an empty/null file-like object? Currently doing this by appending a local blank file I created from my machine but think there should be a better way – awdoibbbbbbb2 Feb 24 '20 at 22:01
  • @awdoibbbbbbb2 Why do that, though? – AMC Feb 25 '20 at 00:54
  • @AMC For a separate portion of my code, I need to open elements from the table_files list as files so I can't append an empty string to the same list. It'll need to be of the same type/format as the other elements in the list which are files – awdoibbbbbbb2 Feb 25 '20 at 06:05
  • Please take a look at my update. – Hai Vu Feb 25 '20 at 17:40