Python Forum
Trying to pathlib instead of os.path
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Trying to pathlib instead of os.path
#2
You are assuming that if it's not a file that it must be a directory. But that's not the case. It might not exist at all. You've only given the filename to Path, not the entire path. So it's not looking in mydir for the files, it's looking in your current directory. When it's not found, is_file() returns False.

Change line 4 to something like dr = pathlib.Path(mydir, esb)

An alternative that is similar:

import pathlib
import os

mydir = 'C:\\02'
for file_object in pathlib.Path(mydir).iterdir():
    if file_object.is_dir():
        print(f"{file_object} is a Directory")
    elif file_object.is_file():
        print(f"{file_object} is a File")
    else:
        print(f"{file_object} is neither file nor directory.")
tester_V likes this post
Reply


Messages In This Thread
Trying to pathlib instead of os.path - by tester_V - Jun-21-2021, 09:06 PM
RE: Trying to pathlib instead of os.path - by bowlofred - Jun-21-2021, 09:29 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Pathlib import not working chriswrcg 9 3,985 May-29-2022, 07:37 PM
Last Post: snippsat
  WebDriverException: Message: 'PATH TO CHROME DRIVER' executable needs to be in PATH Led_Zeppelin 1 2,280 Sep-09-2021, 01:25 PM
Last Post: Yoriz
  deleting an empty Dir, using pathlib.Path tester_V 9 6,060 Jul-01-2021, 01:53 PM
Last Post: Gribouillis
  pathlib destpath.exists() true even file does not exist NaN 9 4,791 Dec-01-2020, 12:43 PM
Last Post: NaN
  Question about if with () or without () / pathlib Tecuma 3 2,307 Apr-02-2020, 10:02 AM
Last Post: Tecuma
  pathlib hanging bluefrog 2 3,220 Sep-25-2018, 12:59 PM
Last Post: volcano63
  pathlib: resolving a path that does not exist Skaperen 6 5,661 Sep-08-2018, 12:25 AM
Last Post: Skaperen
  makin hardlinks with pathlib.Path Skaperen 2 5,314 Sep-06-2018, 07:53 AM
Last Post: scidam
  Need help using pathlib to read text file into dictionary gwilli3 4 4,286 Aug-13-2018, 06:21 PM
Last Post: gwilli3
  How does pathlib.Path.rename work? wavic 7 17,124 Aug-02-2018, 10:58 AM
Last Post: wavic

Forum Jump:

User Panel Messages

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