Issue
My book states that:
Calling
os.listdir(path)
will return a list of filename strings for each file in the path argument.
I tried to get the files inside a folder which is placed on the desktop and it worked perfectly fine. Then I tried to get the files in the root folder '/' and it's giving weird results.
My root folder has 5 files which include Applications, Library, Users etc but os.listdir('/')
gives me a list of some 20-25 list items some of which are Applications, Library, Users,.DS_Store, Trashes, .dbfseventsd,.Spotlight-V100 etc. Note that the bold text list items do not seem to appear in the root folder when I manually open it.
Why is this happening and what should I do?
Solution
Your root folder includes hidden directories or files. These begin with a .
, and are not seen by default in the Finder or ls
. However, os.listdir
returns them as well.
If you want to ignore these files, you may use:
files = [x for x in os.listdir('/') if not x.startswith('.')]
As an extra, it is useful to know how to view these hidden files on OSX. To see them in Finder
:
Open
Finder
Go to your Macintosh HD folder (access this from Devices in the left column)
Hold down
CMD-Shift-.
(dot)
To see them in your terminal, run ls -a /path/to/dir
.
Answered By - cs95
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.