os operating system interfaces module

FunctionsDetails
mkdir()Create directory
walk()path, directories and files of sub -directories

Getting the os name

Name of the operating system.
import os
print(os.name) # nt 

Current working directory

print(os.getcwd()) # c:\Users\DellHP

Change directory

import os
print(os.getcwd()) # c:\Users\DellHP
path='D:\\my_dir'
os.chdir(path)
print(os.getcwd()) # D:\my_dir
Changing to parent directory
os.chdir('..')

OSError

While using above functions, for any system related errors we will get OSError. We can use try except error handling to display message saying error details. Here is a code to raise error while deleting directories.
import os
path='D:\\my_dir1\\my_dir2\\my_dir3\\my_dir4'
#os.makedirs(path) # create all directories in the path
try:
    os.rmdir(path) # delete directory my_dir4 
except OSError as e:
    print(e) # Specific error message 
    print ("Failed to delete %s " % path)
else:
    print ("Successfully deleted the directory %s " % path)
Output ( my_dir4 is not available to delete )
[WinError 2] The system cannot find the file specified: 'D:\\my_dir1\\my_dir2\\my_dir3\\my_dir4'
Failed to delete D:\my_dir1\my_dir2\my_dir3\my_dir4 

remove() : Deleting file

Using file path we can delete the file by using remove()
import os
path='D:\\testing\\my_db\\my_db.db' # update your path
try:
    os.remove(path) # delete directory my_dir4 
except OSError as e:
    print(e) # Specific error message 
    print ("Failed to delete %s " % path)
else:
    print ("Successfully deleted the file %s " % path)
Subscribe to our YouTube Channel here

Subscribe

* indicates required
Subscribe to plus2net

    plus2net.com



    Post your comments , suggestion , error , requirements etc here





    Python Video Tutorials
    Python SQLite Video Tutorials
    Python MySQL Video Tutorials
    Python Tkinter Video Tutorials
    We use cookies to improve your browsing experience. . Learn more
    HTML MySQL PHP JavaScript ASP Photoshop Articles FORUM . Contact us
    ©2000-2022 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer