Forum Discussion
JP8991
1 year agoKudo Commander
Really strange issue - os.listdir
Hey all, Could you kindly look at the below, I have a really weird one where I am referencing a location within my files area of a Lakehouse however sporadically it does not work, I have no idea ...
Anonymous
1 year agoNot applicable
Hi JP8991 ,
You can use this code to list the directories in the Files folder:
import os
# set folder path
folder_path = "/lakehouse/default/Files/"
# list directory
try:
directories = [d for d in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, d))]
# print list
for directory in directories:
print(directory)
except FileNotFoundError:
print(f"The directory {folder_path} does not exist.")
You can use this code to list the directories in the subfolder orders under the Files folder:
import os
# set folder path
folder_path = "/lakehouse/default/Files/orders/"
# list directory
try:
directories = [d for d in os.listdir(folder_path) if os.path.isdir(os.path.join(folder_path, d))]
# print list
for directory in directories:
print(directory)
except FileNotFoundError:
print(f"The directory {folder_path} does not exist.")
They all work well.
If you have any other questions please feel free to contact me.
Best Regards,
Yang
Community Support Team
If there is any post helps, then please consider Accept it as the solution to help the other members find it more quickly.
If I misunderstand your needs or you still have problems on it, please feel free to let us know. Thanks a lot!
frankmroberts
1 year agoFrequent Visitor
I am attempting the same thing and used the code above and am getting the same response "The directory **** does not exist."