PYTHON DIRECTORIES AND FILES MANAGEMENT


DIRECTORIES AND FILES MANAGEMENT


# If there are a large number of files to handle in python, we can arrange our code within different directories to make things more manageable.
# A directory is a collection of files and sub directories.
# Python has the os module that provides many useful methods to work with directories and files.


# get current directory.
getcwd()
# This module returns the current working directory in the form of a string.
# We can also use:
getcwdb() # method to get it as bytes object.

import os
os.getcwd()
os.getcwdb()
print(os.getcwd())

# Changing directory.
chdir()
# We can change the current working directory by using this method.
# The new path that we want to change into must be supplied as a string to this method.
# We can use both forward slash(/) and backward slash (\) to separate path elements.
os.chdir('C:\\Python3')
print(os.getcwd())

# List of directories and files.
# All files and sub directories inside a directory can be retrieved using
listdir() method.
os.listdir()

# Making a new directory.
# We can make a new directory using:
mkdir() method
os.mkdir(
'file name')

# Renaming a directory or a file.
# Used for renaming file or directory.
os.rename('old file name','new one')

# Removing a file.
# Used to remove a file
os.remove('file name to be removed')

# Removing a directory.
# Used to remove a directory.
os.rmdir('the directory to be removed')

# In order to remove a non-empty directory we can use the rmtree method inside the shutil module(used when we access  any directory linked with data base.
import shutil
shutil.rmtree(
'file name')

 #python #programming #coding #programmer #snake #machinelearning #code #coder #ballpython #pythonprogramming #computerscience #datascience #reptile #snakes #reptiles #snakesofinstagram #reptilesofinstagram #technology #pythonsofinstagram #artificialintelligence #tech#directories#electronics#engineering




Comments

Popular posts from this blog

MATRIX BASIC PROGRAMS (PYTHON)