Introduction
Welcome to my 46th blog post on the Python journey. On day 46, I learned about the most important module in Python which is the OS module. On day to day basis we perform many repetitive and mundane tasks on our computers and we would want to remove the hassle of doing boring tasks by automating them. Using the OS module we can automate tasks that we manually do in the Operating system. For example, creating multiple files or folders or copying files etc and many other mundane tasks. Now Let's dive into more details and understand the working of the ' os ' module in Python.
So let's get started......
'os' Module in Python
It is a built-in library that provides functions for interacting with the operating system.
OS module allows you to perform a wide range of tasks, for example reading files and writing into them. Interacting with a file system like renaming files, creating new files or deleting the existing ones. We can also read the contents of the file using the os module.
Example 1 -
import os
# Open the file in read-only mode
f = os.open("myfile.txt", os.O_RDONLY)
# Read the contents of the file
contents = os.read(f, 1024)
# Close the file
os.close(f)
Example 2 -
Use os.O_WRONLY flag, to open the file for writing.
import os
# Open the file in write-only mode
f = os.open("myfile.txt", os.O_WRONLY)
# Write to the file
os.write(f, b"Hello, world!")
# Close the file
os.close(f)
Interacting with the file system
The os module also provides functions for interacting with the file system.
For example use the os.mkdir function to create a new directory:
import os
# Create a new directory
os.mkdir("newdir")
Use the os.listdir function to get a list of the files in a directory:
import os
# Get a list of the files in the current directory
files = os.listdir(".")
print(files) # Output: ['myfile.txt', 'otherfile.txt']
Running system commands
Use the os.system function to run a command and get the output:
import os
# Run the "ls" command and print the output
output = os.system("ls")
print(output) # Output: ['myfile.txt', 'otherfile.txt']
Use the os.popen function to run a command and get the output as a file-like object:
import os
# Run the "ls" command and get the output as a file-like object
f = os.popen("ls")
# Read the contents of the output
output = f.read()
print(output) # Output: ['myfile.txt', 'otherfile.txt']
# Close the file-like object
f.close()
I also wrote a bunch of code and learned how to create multiple folders in a directory, rename the file, change the file names, changing the directory.
import os
if(not os.path.exists("data")):
os.mkdir("data")
for i in range(0, 100):
os.mkdir(f"data/Day{i+1}")
#Renaming files all together
# for i in range(0, 100):
# os.rename(f"data/Day{i+1}", f"data/Tutorial{i+1}" )
# os.rename(f"data/Tutorial{i+1}", f"data/Tutorial {i+1}" )
# import os
# folders = os.listdir("data")
# print(folders)
# print(os.getcwd()) #gives the directory in which we are
# os.chdir("/users") #changes directory
# print(os.getcwd())
# #prints the folder name and the files of each folders.
# for folder in folders:
# print(folder) #print the folders name
# print(os.listdir(f"data/{folder}")) #shows the files in folder
Resources Used
You can watch the video of Day#46 by clicking on the below link ๐๐๐๐๐
Conclusion
Thanks, guys for going through this blog post. On day 46, we learned about the os module which is a built-in library that provides a wide variety of functions for interacting with the operating system. OS module enables us to perform and automate mundane tasks such as working on file systems and system commands.
Thank you if you read this post and have found this post useful. I hope you have joined me and are enjoying my magical journey of Python coding. This is it for Day #46
See you in the next one.....
About Me
Hey Guys, I am Chintan Jain from CodeWithJain. I am a trader and content creator. I am also passionate about tech and hence wanted to explore the field of tech. I always wanted to learn to code so I watched many tutorials but procrastinated practicing coding. To get into the habit of coding consistently I am starting to BLOG with HASHNODE on daily basis.
I will document my coding journey from scratch and share my daily learnings in a blog post on HASHNODE. I hope you all will enjoy my content and my coding journey.
So what are you waiting for, smash the FOLLOW and LIKE buttons and follow along my coding journey, a step to create more beautiful digital products and empower people.