Search This Blog

Tuesday 7 July 2020

Automation using Python

## Sample python script for creating symlinks in different directory locations 

Input file: parameter-list.txt

/home/content/htdocs/lib/, ../../application/lib/core,core,1
/home/content/htdocs/modules/,../application/modules/core,core,2
/home/content/htdocs/resources/,../../resources/pdf,pdf,3

Python script:

# Author: Pavan Bandaru

import os
import shutil

if os.path.exists("/home/scripts/parameter-list.txt"):
    with open("/home/scripts/parameter-list.txt", "r") as file:
        for line in file:
            fields = line.split(",")
            path = fields[0]
            link = fields[1]
            fn = fields[2]
            source = fn
            destination = fn + "_old"
            os.chdir(path)
            if os.path.isfile(fn):
                shutil.move(source, destination)
                os.symlink(link, fn)
            else:
                os.symlink(link, fn)
else:
    print("File doesn't exist")