0

我正在制作所有路径的元组,但它还包含两个额外的路径

  1. /Users/sanjeevkumar/Pictures/.DS_Store
  2. /Users/sanjeevkumar/Pictures/.localized

我如何摆脱上述路径,我正在使用以下技术生成元组路径

tuple(os.path.join(self._path,each) for each in os.listdir(self._path) if os.path.isfile(os.path.join(self._path,each)))
4

2 回答 2

2

使用运算符展开if子句and

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') # <-------------
)
于 2013-11-16T12:31:55.430 回答
0

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.

于 2013-11-17T03:09:51.757 回答