Wednesday, 20 September 2017

Folder Organizer

import os,errno
import sys,getopt
def main(argv):
    inputDir=''
    try:
        opts,args= getopt.getopt(argv,"hi:",["iDir="])
    except getopt.GetoptError:
        print 'DirectoryOrganizer.py -i <inputDirectory>'
        sys.exit(2)
    for opt,arg in opts:
        if opt == '-h':
            print 'DirectoryOrganizer.py -i <inputDirectory>'
            sys.exit()
        elif opt in ("-i","--iDir"):
            inputDir=arg
    files= os.listdir(inputDir)
    for file in files:
        extension= os.path.splitext(file)[1][1:]
        if extension:
            fileInputDir =inputDir+extension
            try:
                if not os.path.isdir(fileInputDir):
                    os.makedirs(fileInputDir)
                if not os.path.isfile(fileInputDir+"\\"+file):
                    os.rename(inputDir+file,fileInputDir+"\\"+file)
                else:
                    print "file with same name already exist in folder "+ fileInputDir+"\\"+file
            except OSError as e:
                if e.errno != errno.EEXIST:
                    raise

if __name__ == "__main__":
main(sys.argv[1:])

Wednesday, 13 September 2017

Allocating Void pointer memory using new operator

I was Implementing Generic List in C++ and was able to successfully allocate memory of void*  data using malloc ( malloc return void *) . But how about using new. Can we do below


void Test()
{
           void *f= new void[25];  //void *f=malloc(25); --> Works
           delete f;
}


The Answer is NO.
Because void has no size . How much space has to be allocated is not known.

So if you need to allocate memory using new use operator like


void Test()
{
          void *f=operator new(25);
          delete f;
}