check directory python code example

Example 1: working directory python

import os
cwd = os.getcwd()

Example 2: get directory of file python

import os 
dir_path = os.path.dirname(os.path.realpath(__file__))

Example 3: os get current directory

import os

#Get Current working Directory
currentDirectory = os.getcwd()

#Change the Current working Directory
os.chdir('/home/varun')

Example 4: check dir exist python

import os.path
from os import path

def main():

   print ("File exists:"+str(path.exists('guru99.txt')))
   print ("File exists:" + str(path.exists('career.guru99.txt')))
   print ("directory exists:" + str(path.exists('myDirectory')))

if __name__== "__main__":
   main()