1

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.

Liam
  • 23
  • 6
  • 2
    No screenhots for this, please copy and paste the formated code (including the snippet with `break`) – Chris_Rands Oct 22 '18 at 10:00
  • also: if you break before, you get the value of your variable before `os.listdir(..)` puts new things into it, if you break afterwards you get the new content ... – Patrick Artner Oct 22 '18 at 10:01
  • Surely I should get the same values back irrespective if I break before or after. If i.e. if I break before, and run the command manually, I get X value, if I break after I should see X value ? – Liam Oct 22 '18 at 10:27

1 Answers1

1

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.

Patrick Artner
  • 50,409
  • 9
  • 43
  • 69
  • I am still only getting the files present in the output for os.walk attempt. Running ` for akt_dir, subdirs, files in os.walk(run_directory): print(akt_dir, subdirs, files)` – Liam Oct 22 '18 at 10:36
  • @Liam Then you should check where exactly you are - if there are directories in the dir you are sitting in, you will get them as well as files. You might want to check your "directories" and see if they are actually symlinks, if so you might need to tweak os.walk() using parameters to follow symlinks as well - see https://docs.python.org/3/library/os.html#os.walk and maybe read into [check-if-file-is-symlink-in-python](https://stackoverflow.com/questions/11068419/check-if-file-is-symlink-in-python) – Patrick Artner Oct 22 '18 at 10:38
  • Thanks for the suggestions. I can see the directories if I browse to them, and the directories are not symlinks, they are created by another process. Using `os.path.isdir` or `isfile` returns True. I did not mention that this is a method inside an object. Perhaps that has something to do with the peculiarity. – Liam Oct 22 '18 at 10:59
  • Turns out the os.walk or os.listdir command was being called too soon after the previous asynchronous command. A time.sleep() caused the right information to be returned. – Liam Oct 23 '18 at 08:18