getting rid of not .DS_Store and .localized in OSx path from listdir
-
21-12-2019 - |
Question
I am making a tuple of all path but it also contains two extra paths
/Users/sanjeevkumar/Pictures/.DS_Store
/Users/sanjeevkumar/Pictures/.localized
how do i get rid of the above paths, I am generating the tuple path using the below technique
tuple(os.path.join(self._path,each) for each in os.listdir(self._path) if os.path.isfile(os.path.join(self._path,each)))
Solution 2
Alternatively I also realized that I can do it this way
tuple(os.path.join(self._path,each)
for each in os.listdir(self._path)
if os.path.isfile(os.path.join(self._path,each))
and each.endswith('png') or each.endswith('jpg')
)
I think this will also help me get rid of any other file extension that could appear and is not compatible.
OTHER TIPS
Expand the if
clause using and
operator.
tuple(
os.path.join(self._path,each)
for each in os.listdir(self._path)
if os.path.isfile(os.path.join(self._path,each))
and each not in ('.DS_Store', '.localized') # <-------------
)
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow