Python Forum
Question about if with () or without () / pathlib
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Question about if with () or without () / pathlib
#1
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
Reply
#2
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.
Reply
#3
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
Reply
#4
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
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Pathlib import not working chriswrcg 9 3,669 May-29-2022, 07:37 PM
Last Post: snippsat
  deleting an empty Dir, using pathlib.Path tester_V 9 5,780 Jul-01-2021, 01:53 PM
Last Post: Gribouillis
  Trying to pathlib instead of os.path tester_V 4 2,476 Jun-22-2021, 04:15 AM
Last Post: tester_V
  pathlib destpath.exists() true even file does not exist NaN 9 4,666 Dec-01-2020, 12:43 PM
Last Post: NaN
  pathlib hanging bluefrog 2 3,114 Sep-25-2018, 12:59 PM
Last Post: volcano63
  pathlib: resolving a path that does not exist Skaperen 6 5,480 Sep-08-2018, 12:25 AM
Last Post: Skaperen
  makin hardlinks with pathlib.Path Skaperen 2 5,222 Sep-06-2018, 07:53 AM
Last Post: scidam
  Need help using pathlib to read text file into dictionary gwilli3 4 4,180 Aug-13-2018, 06:21 PM
Last Post: gwilli3
  How does pathlib.Path.rename work? wavic 7 16,906 Aug-02-2018, 10:58 AM
Last Post: wavic
  pathlib:file in a directory Skaperen 1 2,760 Apr-01-2018, 03:26 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020