Day 12 : Os and Time module in python

Hello guys,
yesterday, we learn files sorter using python Link where we used os library.

Today we will learn about more features of os library.

OS Module :

Os Module is used to perform operating system operations. This module is compatible with all os like windows, Linux and Mac. The functions of this module depend on the operating system.

How to use OS module?

Simply by importing

>>>import os

Some Functions:

system(command)
The command can be anything depending on operating system

example
>>> os.system("cmd") 
This will open command prompt directly


getcwd()
Returns current working directory

example
>>> os.getcwd()
'C:\\Users\\Admin\\AppData\\Local\\Programs\\Python\\Python37'




getlogin()

Return the name of the user logged in on the controlling terminal of the process.

example
>>> os.getlogin()
'Admin'




listdir()
list directories in the current working directory.

example
>>> os.listdir()
['DLLs', 'Doc', 'etc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'NEWS.txt', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'README.rst', 'Scripts', 'share', 'tcl', 'Tools', 'vcruntime140.dll']





mkdir(path)
create directory with directory name and at specified path

example
>>> os.mkdir("Mr.Mephisto")
>>> os.listdir()
['DLLs', 'Doc', 'etc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'Mr.Mephisto', 'NEWS.txt', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'README.rst', 'Scripts', 'share', 'tcl', 'Tools', 'vcruntime140.dll']




rename(src,new_name)
src is original file location with name
new_name is new name given to that directory or file 

example
>>> os.rename("Mr.Mephisto","Mitesh")
>>> os.listdir()
['DLLs', 'Doc', 'etc', 'include', 'Lib', 'libs', 'LICENSE.txt', 'Mitesh', 'NEWS.txt', 'python.exe', 'python3.dll', 'python37.dll', 'pythonw.exe', 'README.rst', 'Scripts', 'share', 'tcl', 'Tools', 'vcruntime140.dll']


for more os libraries visit here



Time Module

Python has a module named time to handle time-related tasks.

How to use to time module?
By importing
>>>import time

Some functions

time()
This function return seconds since epoch.

example:
>>> time.time()
1586175388.3553462




ctime(seconds)
this function that arguments in second and convert that into local time.

example:
>>> seconds=time.time()
>>> localtime=time.ctime(seconds)
>>> localtime
'Mon Apr  6 17:54:09 2020'




sleep(seconds)
This function delays the execution depends on the seconds specified.

example:
>>> time.sleep(3)




localtime(seconds)
This function return struct_time and take seconds as argument

example:
>>> seconds
1586175849.9255273
>>> time.localtime(seconds)
time.struct_time(tm_year=2020, tm_mon=4, tm_mday=6, tm_hour=17, tm_min=54, tm_sec=9, tm_wday=0, tm_yday=97, tm_isdst=0)





gmtime(seconds)
This function return struct_time in UTC 

example:
>>> time.gmtime(seconds)
time.struct_time(tm_year=2020, tm_mon=4, tm_mday=6, tm_hour=12, tm_min=24, tm_sec=9, tm_wday=0, tm_yday=97, tm_isdst=0)



for more such functions visit her

❤❤Quarantine python group link ❤❤

8805271377 WhatsApp

Follow here ❤

@mr._mephisto_ Instagram 

There will be no restrictions just feel free to learn. 

Share and take one more step to share knowledge to others. 

Believe in yourself 🤟 you are awesome. 

Be safe, Be happy😁
Take care of yourself and your family 
Of course watch movies and series🤟😉 

And follow the guidelines of government

Comments

Popular posts from this blog

Day 16 : Pandas Basics

News website using Flask and news API - Working with templates : Part 5

Day 9 : Encapsulation and polymorphism