我正在使用python2.7
并尝试递归地查找子目录中的文件,但pathlib2返回NULL
>>> from pathlib2 import Path
>>> list(Path('~/text-segmentation/data/choi/').glob('**/*.ref'))
[]
而如果在python3
中使用glob模块做同样的事情
>>> import glob
>>> glob.glob('~/text-segmentation/data/choi/**/*.ref', recursive=True)
['./data/choi/2/9-11/34.ref', './data/choi/2/9-11/26.ref', './data/choi/2
/9-11/30.ref']
此外,以下技术也不起作用
>>> for root, dirnames, filenames in os.walk('~/text-segmentation/data/choi/'):
... for filename in fnmatch.filter(filenames, '*.ref'):
... matches.append(os.path.join(root, filename))
>>> matches
[]
我得在python2工作。有什么变通方法吗?
编辑:使用/home/myuser/
而不是~
发布于 2018-07-28 20:33:12
如果您希望能够将~
扩展为您的主目录,则必须使用带有expanduser()
方法的PosixPath
:
list(PosixPath('~/text-segmentation/data/choi/').expanduser().glob('**/*.ref'))
https://stackoverflow.com/questions/51571291
复制相似问题