The program creates a replica of existing directory structure with specified extension files only. The program maintains the directory-sub-directory architecture while executing. User has to provide two arguments, First the path of existing directory,whose replica has to be created and Second, the path of directory, where the replica has to be created. This program is developed to fetch files with ".pl" (perl) extension, while maintaining the directory-sub-directory architecture. User can modify it, with his/her interested file extension, like ".txt", ".doc", etc.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 | #!/usr/bin/python
#===============================================================================
# Creator: ACHAL RASTOGI
# Contact: achal13rastogi@gmail.com
# Date: August 16, 2012 Time: 03:00 Hrs KST
#===============================================================================
import os
import sys
def path_fetch(_seq,_sec):
free=[]
never=[]
tk = _seq
kt = _sec
di = os.listdir("%s"%tk)
for file in di:
if tk[-1] == "/":
never.extend([kt,file])
free.extend([tk,file])
pa = "".join(free)
ap = "".join(never)
if os.path.isdir(pa):
os.system('cp -r %s %s'%(pa,ap))
path_fetch(pa,ap)
free=[]
never=[]
else:
#change the file extension ".pl" with the extension of interest, example ".txt", ".doc", ".xls", etc
if ".pl" in pa:
os.system('cp %s %s'%(pa,ap))
free=[]
never=[]
def main():
fh=sys.argv[1]
two=sys.argv[2]
if os.path.exists(fh):
if os.path.exists(two):
print "Program will EXECUTE"
path_fetch(fh,two)
else:
print "Output path doesn't exist"
else:
print "Path doesn't exists"
sys.exit()
if __name__=="__main__":
main()
|
We the programmers, have a nice habit of keeping things managed, neat, tidy, and well structured. I know what all you must be thinking of.. Okay Okay.. I quit :). Assume a well populated directory, containing all your valuable heavy data files, bin files, temp files, codes, sub-directories, sub-sub directories and every level has its own importance and meaning. Now your boss one day comes to you and ask for all the codes you have written for the data given to you yesterday. Ah.. puzzled !! How can I? Its been graved :( Dont worry, this code will help you.. explore it. Please report bugs and errors.