Reputation: 53
Lets say
DIR2 = /tmp/test'
'dirName = stats
if [ -d /$DIR2/$dirName/* ]
Above is what I have.
It checks if /tmp/test/stats
directory contains any files or directories inside of it.
If it does contain something then I will print out yes.
How can I translate this into python language ?
if os.path.exists(DIR2 + "/" + dirName + ):
print "pass"
Is what I tried so far..
But I know I need one more word at the end of the if statement.
Upvotes: 0
Views: 85
Reputation: 81042
I believe this does what you want without requiring creation of a list with the entire set of filenames in the directory.
hasContents = False
for (root, dirs, files) in os.walk(DIR2 + "/" + dirName):
if dirs or files:
hasContents = True
break
Upvotes: 0
Reputation: 310187
I suppose that the easiest way is to check if the list returned by os.listdir
is non-empty...
if os.listdir(os.path.join(DIR2, dirName)):
print("Not empty!")
Upvotes: 2