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('..')

Directory Listing by listdir()

Returns a list of directories and files of given path. If input path is not given then a list of files and diectoris of current working directory is returned.
import os
path = "E:\\testing\\images\\"  # Directory Path
print(os.listdir(path))

expanduser

Users home directory
import os
print(os.path.expanduser('~\\test.txt'))
Output
C:\Users\HP\test.txt

Checking Path

Return True if path or open file decription is avilable. Return False otherwise.
import os
path = "E:\\testing\\images\\"  # Directory Path
print(os.path.exists(path)) # True
For a file
import os
path = "E:\\testing\\images\\test3.png"  #  file path
print(os.path.exists(path)) # True

isdir()

Returns True for directory.
import os
path = "E:\\testing\\images\\test3.png"  # File Path
print(os.path.isdir(path)) # False
path = "E:\\testing\\images\\"  # Directory Path
print(os.path.isdir(path)) # True

splitext()

Split the path into a pair. path=root + ext
import os
path = "E:\\testing\\images\\test3.png"  #  Path
print(os.path.splitext(path)) # ('E:\\testing\\images\\test3', '.png')
print(os.path.splitext(path)[0])  #E:\testing\images\test3
print(os.path.splitext(path)[1])  #.png

Get file creation and file modifiction date and time

Use strftime() to get different date and time format output. Here getmtime() returns the file modified timestamp and getctime() returns file creation timestamp.
import os
from datetime import datetime
path = "E:\\testing\\images\\test3.png"  # Directory Path
t_stamp=os.path.getmtime(path) # for file modificaton time
#t_stamp=os.path.getctime(path) # for file Creation time
dt_mod = datetime.fromtimestamp(t_stamp) # date object 
print('File Modified on:', dt_mod)  # Prting date and time 
m_date = datetime.strftime(dt_mod, '%Y-%m-%d') # Change format 
print(m_date)

File size

In below code we can include this line with getsize() to get the file size in bytes.
size=os.path.getsize(path+f)

File Modified date, file extension and file size

Collecting all files with details from the input directory and creating DataFrame Part II
import os
from datetime import datetime
#path = "E:\\testing\\images\\test3.png"  # Directory Path
path = "E:\\testing\\"  # Directory Path
files=os.listdir(path)
for f in files:
    t_stamp=os.path.getmtime(path+f) # for file modificaton time
    #t_stamp=os.path.getctime(path) # for file Creation time
    f_name,f_extension=os.path.splitext(path+f)
    size=os.path.getsize(path+f)
    dt_mod = datetime.fromtimestamp(t_stamp) # date object 
    #print('File Modified on:', dt_mod)  # Prting date and time 
    m_date = datetime.strftime(dt_mod, '%Y-%m-%d') # Change format 
    print(f, f_extension, m_date,size)
Above code is integrated inside Tkinter GUI

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)

Printing to printer

import os
path = "E:\\testing\\images\\test.txt"  # path to file. 
os.startfile(path, "print")

Printing PDF file

Install these libraries
import win32api
import win32print 
# A List containing the system printers
all_printers = [printer[2] for printer in win32print.EnumPrinters(2)]
# Update the default printer or ask user to select.
win32print.SetDefaultPrinter(all_printers[2]) # Update your printers

path2 = "F:\\testing\\images\\certificate_12.pdf" # Path of  PDF file
win32api.ShellExecute(0, "print", path2, None, ".", 0)

Checking links in all pages of a directory

import re,os
file_name_to_check='pb-check-demo.php'

path1='C:/xampp/htdocs/example/dir_name/' # dir path

file_list=os.listdir(path1)
if(file_name_to_check in file_list):
    print('File is there, checking')
    for f in file_list:
        if(os.path.getsize(path1+f)>500):
            filename,file_extension=os.path.splitext(path1+f)
    
            if(file_extension=='.php'): # check this extension only
                fob= open(path1+f,'r',encoding='utf8',errors='ignore')
                data=fob.read() # collect data 
                fob.close() # close file object 
                urls1 = re.findall(r'href=[\'"]?([^\'" >]+)', data)
                if(file_name_to_check in urls1):
                    print(f)
else:
    print('File is not there, check the name and then submit. ')

Check presence of string inside all files of a directory

Inside the directory check for file names having tkinter and inside the file not having the string www.youtube.
We can use regular expression or find() to search for strings.
import re,os
path1='C:/xampp/htdocs/dir_name/python/' # dir path
file_list=os.listdir(path1) # List of files 
for f in file_list:
    #if re.search('tkinter',f, re.IGNORECASE):
    if f.lower().find('tkinter')>=0: # check string in file name
        #print(f)
        fob= open(path1+f,'r',encoding='utf8',errors='ignore')
        data=fob.read().lower() # collect data 
        fob.close() # close file object 
        if data.find('www.youtube')<0: # not present 
            print(f)

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-2023 plus2net.com All rights reserved worldwide Privacy Policy Disclaimer