Skip to main content

Posts

Showing posts from August, 2016

Python Program to Print Directory Structure Recursively

os.walk - Recursively Generate the file names in a directory tree by walking the tree either top-down or bottom-up. For each directory in the tree rooted at directory top (including top itself), it yields a 3-tuple (dirpath, dirnames, filenames). Reference - https://docs.python.org/2/library/os.html Python Program - #!/usr/bin/python3 #Python Program to Print Directory and Files Recursively import os def print_dir(path):   for dirName,subdirList,fileList in os.walk(path):     nw_path = dirName.split('/')     print("|",(len(nw_path))*"---","[",os.path.basename(dirName),"]")     for fl in fileList:       print("|",len(nw_path)*"---","->",str(fl)) if __name__ == "__main__":   path = input("Enter the directory path :-")   print_dir(path) Output - surendra@Surendra:~/workspace/Python_Programs$ python3 recurse_print.py Enter the directory path :-/home/surendra

Program to validate IPv4 Private Address and print the class

#!/usr/bin/python3  #Program to Validate the IP Address and Print the class of the IPV4 Address #  - 10.0.0.0 - 10.255.255.255 -Class A - NetId 8 Bits , Host Id 24 Bits  #   - 172.16.0.0 - 172.31.255.255 - Class B - NetID 12 Bits, Host ID 20 Bits  #   - 192.168.0.0 - 192.168.255.255 - Class C - NetID 16 Bits , Host ID 16 Bits  #   - 127.0.0.0 to 127.255.255.255 - LocalHost / LoopBack  ''' Steps -         Read Input         Validate IP         split and map the input to integer         From list items 0 to 3 , check for IP Range Conditions ''' import re def validate_ip(ip_addr):  if re.search('\d{1,3}\.\d{1,3}\.\d{1,3}\.\d{1,3}',ip_addr):           lst = list(map(int,ip_addr.split('.')))                      #CLASS A Range - 10.0.0.0 - 10.255.255.255       if((lst[0] == 10) and (lst[1] >= 10 and lst[1] <= 255) and (lst[2] >= 0 and lst[2] <= 255)  and (lst[3] >= 0 and lst[3] <= 255)):             print(