Search This Blog

Showing posts with label Directory navigation. Show all posts
Showing posts with label Directory navigation. Show all posts

Tuesday, March 14, 2017

Print files recursively in python

import os

""" Print files recursively in a tree"""
path_dir = raw_input("Enter the directory path: ")
for root, subdirs, files in os.walk(path_dir):
    level=" "*len(root.split("\\"))
    print "%s|_ %s" %(level,root)
    if subdirs:
        pass
    if files:
        for file in files:
            print "%s|___ %s" %(level, file)
    else:
        pass



Output:

C:\Program Files (x86)>C:\Python27\python.exe D:/Deepti/Work/Python/practice/recursive_dir.py
Enter the directory path: D:\Deepti\Work\Python\practice\Dir
      |_ D:\Deepti\Work\Python\practice\Dir
       |_ D:\Deepti\Work\Python\practice\Dir\dir1
       |___ text11.txt
       |___ text12.txt
        |_ D:\Deepti\Work\Python\practice\Dir\dir1\dir11
        |___ text111.txt
        |___ text112.txt
        |_ D:\Deepti\Work\Python\practice\Dir\dir1\dir12
        |___ fiel122.txt

Monday, September 28, 2015

Directory Navigation in python - Script

import os,re,shutil,fnmatch

ex_path="D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\Glob"

# Clean up
if not os.path.exists(ex_path):
    os.mkdir(ex_path)
else:
    shutil.rmtree(ex_path)
    os.mkdir(ex_path)

os.chdir(ex_path)

# Pre-Requisites
dir_list=[]

for i in range(0,4):
    dir_name="dir_%d" %i
    os.mkdir(dir_name)
    dir_list.append([dir_name])

for directory in dir_list:
    print directory[0]
    dir_path=ex_path+"\\" + directory[0]
    print dir_path
    os.chdir(dir_path)
    for i in range(0,4):
        fdesc=open("file_%s_%s" %(directory[0],i),"w")
        fdesc.close()
 
os.mkdir("sub_directory")
change_path=ex_path+"\dir_3\sub_directory"
print change_path
os.chdir(change_path)
fdesc=open("subdirectory.txt","w")
fdesc.close()

for dirpath, dirs, files in os.walk(ex_path):
    path = dirpath.split("\\")
    print len(path)
    print '|',(len(path))*'---','[',os.path.basename(dirpath),']'
    for f in files:
        print "|",(len(path))*'---',f

     
OUTPUT: ======= >>> ================================ RESTART ================================ >>> dir_0 D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\Glob\dir_0 dir_1 D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\Glob\dir_1 dir_2 D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\Glob\dir_2 dir_3 D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\Glob\dir_3 D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\Glob\dir_3\sub_directory 6 | ------------------ [ Glob ] 7 | --------------------- [ dir_0 ] | --------------------- file_dir_0_0 | --------------------- file_dir_0_1 | --------------------- file_dir_0_2 | --------------------- file_dir_0_3 7 | --------------------- [ dir_1 ] | --------------------- file_dir_1_0 | --------------------- file_dir_1_1 | --------------------- file_dir_1_2 | --------------------- file_dir_1_3 7 | --------------------- [ dir_2 ] | --------------------- file_dir_2_0 | --------------------- file_dir_2_1 | --------------------- file_dir_2_2 | --------------------- file_dir_2_3 7 | --------------------- [ dir_3 ] | --------------------- file_dir_3_0 | --------------------- file_dir_3_1 | --------------------- file_dir_3_2 | --------------------- file_dir_3_3 8 | ------------------------ [ sub_directory ] | ------------------------ subdirectory.txt >>> >>> os.stat("D:\Deepti\SPSE-Course-DVD\Exercises\sample.xml" ) nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=219334L, st_atime=1441614421L, st_mtime=1441614386L, st_ctime=1441614421L) >>> os.lstat("D:\Deepti\SPSE-Course-DVD\Exercises\sample.xml") nt.stat_result(st_mode=33206, st_ino=0L, st_dev=0, st_nlink=0, st_uid=0, st_gid=0, st_size=219334L, st_atime=1441614421L, st_mtime=1441614386L, st_ctime=1441614421L)


Directory Navigation in Python - Command line

import os
import glob
import shutil
import fnmatch
import re


ex_path="D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\Glob"
#Cleanup
print "Cleaning up"
if os.path.exists("D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\NewDirectory"):
    os.rmdir("D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\NewDirectory")

if os.path.exists(ex_path):
    # Remove directory and all its contents
    shutil.rmtree(ex_path)

print "Display the current directory path"
print "   %s" %os.getcwd()

print "Create a new directory"
os.mkdir("D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\NewDirectory")

print "List contents of directory"
print os.listdir("D:\Deepti\SPSE-Course-DVD\Exercises\Module 2")

for item in os.listdir("D:\Deepti\SPSE-Course-DVD\Exercises\Module 2"):
    if os.path.isfile(item):
        print "  %s is a file" %item
    elif os.path.isdir(item):
        print "  %s is a directory" %item
    else:
        print "  %s: Unknown filetype" %item
     
print "Remove directory"
os.rmdir("D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\NewDirectory")

print "List contents of directory"
print os.listdir("D:\Deepti\SPSE-Course-DVD\Exercises\Module 2")



# Pre-requisites

os.mkdir("D:\Deepti\SPSE-Course-DVD\Exercises\Module 2\Glob")


file1=open(os.path.join(ex_path,"pythonfile.py"),"w")
file1.close()

file2=open(os.path.join(ex_path,"textfile.txt"),"w")
file2.close()

file3=open(os.path.join(ex_path,"swapfile.swp"),"w")
file3.close()

print "List contents of directory"
print os.listdir(ex_path)


# Exercise on glob
print "---- Print all the python files ----"
print glob.glob(os.path.join(ex_path,"*.py"))

# Exercise on fnmatch
print "---- Match file pattern using fnmatch ----"
print fnmatch.fnmatch(os.path.join(ex_path,"swapfile.swp"),"*.swp")
print fnmatch.fnmatchcase(os.path.join(ex_path,"SWAPFILE.swp"),"*.swp")
print fnmatch.fnmatchcase(os.path.join(ex_path,"waspfile.swp"),"*.swp")

# Prints False
print fnmatch.fnmatchcase(os.path.join(ex_path,"swapfile.SWP"),"*.swp")

fnmatch_pattern=fnmatch.translate("*.swp")
print fnmatch_pattern

reobj=re.compile(fnmatch_pattern)
print reobj

print reobj.match('swap.swp')

# Print set of files from a list of files
Files=['__init__.py', 'fnmatch_filter.py', 'fnmatch_fnmatch.py', 'fnmatch_fnmatchcase.py', 'fnmatch_translate.py', 'index.rst']
print 'Matches:', fnmatch.filter(Files,"*.py")