Python Forum

Full Version: Question about if with () or without () / pathlib
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello Community,

I am using
- macOS 10.15.4
- Python 3.8.2 (via Mac Ports)

In the macOS security settings the terminal has full disk access.

I try to check if a certain path and file exists. When I use () in my if statement using pathlib I got false. When I do not use () in the if statement I got true.

Can someone please explain this difference when using pathlib.
Does someone has similar setup and can say if os.path.exists is working or not.

My code:

import os
import platform
from pathlib import Path

path_Safari = "~/Library/Safari/"
path_Safari_Bookmark = "~/Library/Safari/Bookmarks.plist"


def check_os_version():
    print("Check OS version")
    print("================")
    print("")

    os_general = platform.system()
    print("My OS is:", os_general)

    os_release = platform.release()
    print("My OS release is:", os_release)
    print("")

    if os_general == "Darwin":
        print("I have to do something")
        print("")
        if os.path.exists(path_Safari):
            print("Path: " + path_Safari + "exist")
            print("")
        else:
            print("Path: " + path_Safari + " does not exist or is not accessible. Please check")
            print("")
        if os.path.exists(path_Safari_Bookmark):
            print("Path: " + path_Safari_Bookmark + "exist")
            print("")
        else:
            print("Path: " + path_Safari_Bookmark + " does not exist or is not accessible. Please check")
            print("")

    my_dir = Path(path_Safari)
    if my_dir.is_dir():
        print("pathlib dir: OK")
        print("")
    else:
        print("pathlib dir: NOT OK")
        print("")

    my_file = Path(path_Safari_Bookmark)
    if my_file.is_file:
        print("pathlib file: OK")
        print("")
    else:
        print("pathlib dir: NOT OK")
        print("")



check_os_version()
My result:
Quote:Check OS version
================

My OS is: Darwin
My OS release is: 19.4.0

I have to do something

Path: ~/Library/Safari/ does not exist or is not accessible. Please check

Path: ~/Library/Safari/Bookmarks.plist does not exist or is not accessible. Please check

pathlib dir: NOT OK

pathlib file: OK

Regards

—Christian
Path.is_file() is a method. Without parentheses, the interpreter returns the memory location of the method, which in turn evaluates to True in a conditional. To run the method and actually check if the file exists, you need the parentheses.
Hello stullis,

thank you for your explanation. Now I understand why I got "True" when I do not use ().

I guess then that the "False "result comes from further restrictions from macOS. I try to test my code on another OS.

Regards

--Christian
Hi Community,

after some tests I found my "error". I was working with "~" in the paths. It seems that the libraries os and pathlib can not handle "~". When I work with absolute path my code is working as expected / needed.

Regards

--Christian