Introduction

Setup

Operators

variable

String

Number

Dtype & Comment

Lists

Tuples

Sets

Dictionaries

if else statement

For Loop

While Loop

iterator & iterable

Pre-Built functions

Function

Decorators

Generators

Types of errors

Exceptions Handling

OOP

Regular expression

Create module

OS module

Date module

Random module

Math

Statistics module

Exercise 1

Exercise 2

Exercise 3

Learn Data Structure & Algorithm

Learn Numpy

Learn Pandas

Learn Matplotlib

Learn Seaborn

Learn Statistics

Learn Math

Learn MATLAB

Learn Machine learning

Learn Github

Learn OpenCV

Learn Deep Learning

Learn MySQL

Learn MongoDB

Learn Web scraping

Learn Excel

Learn Power BI

Learn Tableau

Learn Docker

Learn Hadoop

Python os module

Python os module various operations

how to see current directory location?

Input
os.chdir("C:/Users/")
#chdir() function is used to change the current directory

print(os.getcwd())

How to see all the folders name present in the current directory?

Input
os.chdir("D:/")
print(os.listdir())
"""
listdir() function makes a list of the folder or files present in the current directory.
"""

How to create file or folder in a particular location?

To do this You have two functions:
1.makedirs():
If we want to create folder inside folder(like myfilder/subfol/main folder) then use this function.

2.mkdir():
If we simply want to create a folder then use this function. But we can also use makedirs() function to create a folder.

Input
os.chdir("D:/")
os.makedirs("my files/sub my files/main file")
print(os.listdir())
#os.mkdir("my files")

How to delete a file or folder?

Input
#To remove directory removedirs() function is used
os.chdir("D:/")
os.removedirs("my file/sub my file/main file")
print(os.listdir())

How to rename a folder or file?

Input
os.chdir("D:/") os.rename("my file","My_file") '''
In rename function, at first, we have to pass the current name and then the new name
'''
print(os.listdir())

How to get information about a folder or file?

Input
os.chdir("D:/")
print(os.stat("My_file"))
'''
This function will give we information(like file size, last modification time, etc) about the given file.
'''

How to get files or folder tree of a particular directory?

Suppose in E drive we have two folders. Inside these folders, we have some folders and files, and also inside these folders, we have some folders. Let's see that in E drive how many files or folders are present, the name of these folders or files, and folder location. To do this we will use for loop and walk() function. Inside of the walk function, we will pass the path.

Input
for current_dir_path,dir_name,filenames in os.walk("E:/"):
  print("Current path:",current_dir_path)
  print("Directories:",dir_name)
  print("Files:",filenames)
  print()

How to get environment variable?

Input
os.chdir("C:/User/HP/")
os.environ
'''
This will print all the environment variable names and locations.

We can get a particular environment variable using the get function. Inside get function to pass the variable name
'''
os.environ.get("My_env")

CodersAim is created for learning and training a self learner to become a professional from beginner. While using CodersAim, you agree to have read and accepted our terms of use, privacy policy, Contact Us

© Copyright All rights reserved www.CodersAim.com. Developed by CodersAim.