Search code examples
pythonpython-3.xlistdir

Python os.listdir is not giving consistent output


I am using Python 3.6.2 in a Conda virtual environment, on Linux Ubuntu 18.04.

I have tried several ways to list both the files and directories of a particular path but I every method I try seems to only list the files in a directory, not the files and directories.

My code contains

directory_contents = os.listdir(run_directory)
print(directory_contents)

which shows only

['170224-ARC122-1-uM-Cis-S1-subsample_R1_001.fastq.gz', '170224-ARC122-1-uM-Cis-S1-subsample_R2_001.fastq.gz']

If I call a break before the listdir command, and then step through the listdir command the variable is filled with the correct contents

ipdb.set_trace()
print(directory_contents)
directory_contents = os.listdir(run_directory)
print(directory_contents)

*** NameError: name 'directory_contents' is not defined

['170224-ARC122-1-uM-Cis-S1-subsample_R1_001.fastq.gz', 'bw', 'Stats', 'bwChrM', 'bg', '170224-ARC122-1-uM-Cis-S1-subsample_R2_001.fastq.gz', 'bgChrM', 'Log']

Calling the break after the listdir command

directory_contents = os.listdir(run_directory)
ipdb.set_trace()
print(directory_contents)

gives

['170313-ARC122-no-Cis-S5-subsample_R2_001.fastq.gz', '170313-ARC122-no-Cis-S5-subsample_R1_001.fastq.gz']

What is it that I am not understanding or what extra keyword/argument have I overlooked ? Why am I getting different results depending on if I break before the command or afterwards?

The apparent simplicity of this seems hardly worth posing as a question but I have run out of solutions.


Solution

  • You could use the first result of os.walk():

    import os
    
    # demo structure: 5 dirs, 5 files
    for n in range(5):
        os.mkdir(f"dir_{n}")
    
    for n in range (10,15):
        with open(f"file_{n}.txt","w") as f:
            f.write("...")
    
    # query
    akt_dir, subdirs, files = next(os.walk("./")) # get the first result of the generator
    print(akt_dir)
    print(subdirs)
    print(files)
    

    Output:

    ./
    ['dir_0', 'dir_2', 'dir_3', 'dir_4', 'dir_1']
    ['file_14.txt', 'file_10.txt', 'file_12.txt', 'main.py', 'file_13.txt', 'file_11.txt']
    

    os.walk() returns a generator - so it might query more then you want.