您好,为什么当我使用相对路径时,file exists()返回false,而当路径为绝对路径时返回true?谢谢

x0fgdtte  于 2021-08-20  发布在  Java
关注(0)|答案(1)|浏览(366)
import pathlib

home1 =pathlib.Path("/Users/sergii/Desktop/hello.txt")
print(f"home 1 is located {home1.cwd()}")  #home 1 is located /Users/sergii/Documents
print(f"home 1 exists {home1.exists()}")   #home 1 exists True
print(f"home 1 is a file {home1.is_file()}\n")

home2 = pathlib.Path("Desktop/hello.txt")
print(f"home 2 is located {home2.cwd()}")  #home 2 is located /Users/sergii/Documents
print(f"home 2 exists {home2.exists()}")   #home 2 exists False
print(f"home 2 is a file {home2.is_file()}")
cuxqih21

cuxqih211#

第二个示例是解析相对路径 Desktop/hello.txt 关于当前工作目录,所以它正在检查 /Users/sergii/Documents/Desktop/hello.txt 存在。

相关问题