DEV Community

Cover image for 6 Helpful OS Module Methods
Aya Bouchiha
Aya Bouchiha

Posted on

4 1

6 Helpful OS Module Methods

Hello,
I'm Aya Bouchiha,
today,
I'm going to share with you 6 useful os module methods.
Firstly, we need to know that the os module is built-in in python, it is used for interacting with the operating system.

getcwd()

getcwd(): this method helps you to get the absolute path of the current working directory as a string.

import os

print(os.getcwd())
# C:\Users\AyaBouchiha\Desktop\posts
Enter fullscreen mode Exit fullscreen mode

os.mkdir()

mkdir(): lets you create a new directory if it does not exists, else it raises an error (FileExistsError)

import os

os.mkdir('python') # done :)

os.mkdir('python') # error (FileExistsError)
Enter fullscreen mode Exit fullscreen mode

os.rmdir()

rmdir(): helps you to remove an empty directory.

import os

# done
os.mkdir('python/newFolder')  

# done
os.rmdir('python/newFolder')  

# error (FileNotFoundError)
os.rmdir('something/does-not-exists') 

# error (NotADirectoryError)
os.rmdir('user.json') 
Enter fullscreen mode Exit fullscreen mode

os.remove()

remove(): lets you delete a file. If it does not exists, this method raises an error.

import os

# done
os.remove('posts/python/test.py')

# error(FileNotFoundError)
os.remove('posts/python/test.py')
Enter fullscreen mode Exit fullscreen mode

os.path.isfile()

isfile(): returns True if the given path is a regular file, otherwise It returns False.

import os


my_file_path = 'posts/python/os_module.py'
# True
print(os.path.isfile(my_file_path))

file_does_not_exists_path = 'posts/python/os_module.json'
# False
print(os.path.isfile(file_does_not_exists_path))

directory_path = 'posts/python'
# False
print(os.path.isfile(directory_path))
Enter fullscreen mode Exit fullscreen mode

os.listdir()

listdir(): returns a list that contains all files and directories names that exist in the specified path.

import os

print(os.listdir('posts/'))
# Output:
# [
#  '5_list_python_methods.md',
#  'js_resources.md',
#  'MapObject.md',
#  'os_module.md',
#  'os_module.py',
#  'python'
# ]
Enter fullscreen mode Exit fullscreen mode

Summary

  • getcwd(): returns the absolute path of the current working directory as a string.

  • mkdir(): creates a new directory.

  • rmdir(): removes an empty directory.

  • remove(): deletes a file.

  • isfile(): returns True if the given path is a regular file.

  • listdir(): returns a list that contains all files and directories names that exist in the specified path.

Suggested posts

To Contact Me:

Have a great day!

Heroku

This site is built on Heroku

Join the ranks of developers at Salesforce, Airbase, DEV, and more who deploy their mission critical applications on Heroku. Sign up today and launch your first app!

Get Started

Top comments (0)

A Workflow Copilot. Tailored to You.

Pieces.app image

Our desktop app, with its intelligent copilot, streamlines coding by generating snippets, extracting code from screenshots, and accelerating problem-solving.

Read the docs

👋 Kindness is contagious

Please leave a ❤️ or a friendly comment on this post if you found it helpful!

Okay