小编mhe*_*mmy的帖子

将 WindowsPath 转换为字符串

redpath = os.path.realpath('.')              
thispath = os.path.realpath(redpath)        
fspec = glob.glob(redpath+'/*fits')
thispath = os.path.realpath(thispath+'/../../../..')
p = Path(thispath)
userinput = 'n'
while (userinput == 'n'):
   text_file = next(p.glob('**/*.fits'))
   print("Is this the correct file path?")
   print(text_file)
   userinput = input("y or n")

parent_dir = text_file.parent.resolve()
fspec = glob.glob(parent_dir+'/*fits')
Run Code Online (Sandbox Code Playgroud)

我收到错误

unsupported operand type(s) for +: 'WindowsPath' and 'str'
Run Code Online (Sandbox Code Playgroud)

我认为这是因为当我需要 glob 一个字符串时,我试图 glob 一个 Windows 文件路径。有没有一种方法可以将 WindowsPath 转换为字符串,以便我可以将所有文件合并到一个列表中?

python glob filepath pathlib

17
推荐指数
1
解决办法
2万
查看次数

标签 统计

filepath ×1

glob ×1

pathlib ×1

python ×1