Forum Discussion
Really strange issue - os.listdir
For the use case I need it for, which is to list what files are listed in a directory, I do not think this is correct.
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!
- JP89911 year agoKudo Commander
I genuinely think there is some sort of bug.
I ran your code and this was my outcome:
I then ran the code three minutes later and this was my outcome which is exactly as expected: - frankmroberts1 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."