3

The following Python script:

import os
print os.listdir('D:\images')

is outputing the names of all the folders in the D:\image directory, but it is also showing desktop.ini in the same folder, while there is no such file in the image directory.

Its also not a hidden item, I am sure of that.

Why is it then showing it as a content?

a2800276
  • 3,272
  • 22
  • 33
Nancy
  • 315
  • 1
  • 5
  • 16

2 Answers2

2

desktop.ini is a protected system file, and Windows tends to hide it.

You can verify by going to D:\images in a terminal and running dir /A.

See this answer as well.

You can use os.walk() if you want more control, it will give you directories and files separately. You can also use os.path.isdir() to find out if an entry you get is a directory.

Community
  • 1
  • 1
Vlad
  • 18,195
  • 4
  • 41
  • 71
  • Can I delete it from there? Its causing my program to halt before scanning all the folders in D:\images\ . – Nancy Aug 26 '15 at 11:38
  • added some edits. it's safe to delete, but Windows might add it back. the correct thing to do is to handle it in a predictable way. – Vlad Aug 26 '15 at 11:50
  • Can you tell how I could have used os.walk() to determine if the item is a directory or not? – Nancy Aug 26 '15 at 12:27
  • For each directory, it gives you a `(dirpath, dirnames, filenames)` tuple, in which directories and files are separate. It will also walk the whole tree. – Vlad Aug 26 '15 at 12:32
0

I used os.path.isdir(path) to check whether the item returned by os.listdir() is a directory or not

This way,desktop.ini didn't meet the criteria of being a directory and my program scanned all the folders.

Nancy
  • 315
  • 1
  • 5
  • 16