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()}")
1条答案
按热度按时间cuxqih211#
第二个示例是解析相对路径
Desktop/hello.txt
关于当前工作目录,所以它正在检查/Users/sergii/Documents/Desktop/hello.txt
存在。