Category: Directories
Python : Get Current Directory
In this article, we will discuss how to get the current working directory in Python. Python’s os module provides a function to get the current working directory i.e. os.getcwd() It returns a string containing the current working directory. For calling this function we need to import os module i.e. Advertisements import os Let’s understand by …
Python : How to get list of files in directory and sub directories
In this article we will discuss different methods to generate a list of all files in directory tree. Creating a list of files in directory and sub directories using os.listdir() Python’s os module provides a function to get the list of files or folder in a directory i.e. Advertisements os.listdir(path=’.’) It returns a list of …
Python : How to get list of files in directory and sub directoriesRead More
Python : How to check if a directory is empty ?
In this article we will discuss different ways to check if a directory is empty or not. Python’s os module provides a function to get the list of files or folder in a directory i.e. Advertisements os.listdir(path=’.’) It returns a list of all the files and sub directories in the given path. Now if the …
Python : How to Get List of all empty Directories ?
In this article we will discuss how to get list of all empty directories. Python’s os module provides a function to iterate over a directory tree i.e. Advertisements os.walk(path) It iterates of the directory tree at give path and for each directory or sub directory it returns a tuple containing, (<Dir Name> , <List of …
Python : How to Get List of all empty Directories ?Read More
Python : How to move files and Directories ?
In this article we will discuss different ways to move files and directories in python. In python we have a shutil module that provides various files related operations. To import the shutil module use following line, Advertisements import shutil It also provides functions to move files i.e. shutil.move(src, dst) It accepts source and destination path as …
Python : How to copy files from one location to another using shutil.copy()
In this article we will discuss how to copy files from one directory to another using shutil.copy(). shutil.copy() Python’s shutil module provides a function shutil.copy() i.e. Advertisements shutil.copy(src, dst, *, follow_symlinks=True) It copies the file pointed by src to the directory pointed by dst. Parameters: Frequently Asked: Python : How to remove files by matching …
Python : How to copy files from one location to another using shutil.copy()Read More
Python : How to remove files by matching pattern | wildcards | certain extensions only ?
In this article we will discuss how to delete few files from a directory based on matching pattern or wildcard. Suppose we have a directory that contains some log files and some text files and we want to delete all .txt files from that directory i.e. files that ends with string “.txt”. Let’s discuss how …
Python : How to remove files by matching pattern | wildcards | certain extensions only ?Read More
Python : How to get Last Access & Creation date time of a file
In this article we will discuss different ways to get the last access and creation timestamp of a file and how to convert them into different formats. os.stat() Python’s os module provides a function os.stat() Advertisements os.stat(pathOfFile) It accepts the path of file as argument and returns the status of file in the form of an …
Python : How to get Last Access & Creation date time of a fileRead More
Python : How to remove a file if exists and handle errors | os.remove() | os.ulink()
In this article we will discuss how to remove a file if only it exists and how to handle other types of exceptions using os.remove() & os.ulink(). How to remove a file using os.remove() python ‘s os module provides a function to remove the file i.e. Advertisements os.remove(path_of_file) It accepts the file path as argument …
Python : How to remove a file if exists and handle errors | os.remove() | os.ulink()Read More
Python : How to delete a directory recursively using shutil.rmtree()
In this article we will discuss how to delete an empty directory and also all contents of directory recursively i.e including contents of its sub directories. Delete an empty directory using os.rmdir() Python’s os module provide a function to delete an empty directory i.e. Advertisements os.rmdir(pathOfDir) Path of directory can be relative or absolute. It …
Python : How to delete a directory recursively using shutil.rmtree()Read More