pathlib 打印当前目录路径

pathlib prints the current directory path

import os
import sys
import pathlib
for folderName,subfolders,filenames in os.walk('/'):
    for filename in filenames:
    #   print(filename)
        if filename.endswith('.pdf'):
            path=pathlib.Path(filename).parent.absolute()
            print("the file "+str(filename)+" has path "+str(path))

这应该有效:

import os
import sys
import pathlib
    for folderName,subfolders,filenames in os.walk('/'):
        for filename in filenames:
            if filename.endswith('.pdf'):
                print(f"the file {filename} has path {folderName}")

你不需要这个路径库。 pathlib.Path(filename) 会将文件名视为相对路径,因此其父级将是运行脚本的文件夹。