Tutorials  Articles  Notifications  Login  Signup


RK

Rajan Kumar

Founder at HackersFriend Jan. 20, 2023, 1:54 p.m. ⋅ 2412 views

Python - check if a path is file or directory


To check whether given path is file or a directory we can use Python’s isdir() and isfile() functions. See below examples:

>>import os
>>given_path = “/home/rajan/
>>os.path.isdir(given_path)
True
>>os.path.isfile(given_path)
False
>>given_path2 = ‘/home/rajan/sample.txt’
>>os.path.isfile(given_path2)
True

 



HackerFriend Logo

Join the community of 1 Lakh+ Developers

Create a free account and get access to tutorials, jobs, hackathons, developer events and neatly written articles.


Create a free account