Python – Search for specific types of files and rename the files or part of the files

The script finds files with a specific file extension and renames parts of the files it found.

import os
import re
 
src_drct='/home/gmastrokostas/tmp'
 
for files in os.listdir(src_drct):
 
    if files.endswith('.txt'): #Select all files with the .txt ext
        oldF = os.path.join(src_drct, files) #Join full path with files found
        midF = re.split(r'\_', files) #split files that contain underscore
        newF = oldF.replace('_01012015', '_01012014') #create the replacement var
        out = os.rename(oldF, newF) #start renaming.
Share Button

Python – Change one specific extension type of files in a directory that contains multiple types of extensions

import os
import re

src_drct='/home/gmastrokostas/tmp'

for files in os.listdir(src_drct):

    if files.endswith('.txt'):
        oldF = os.path.join(src_drct, files)
        #midF = re.split(r'\.', files)#This works too.
        midF = os.path.splitext(files) #It creates a list and splits the name of the file from the extension.
        newF = oldF.replace('.txt', '.py')#Simple replacement.
        out = os.rename(oldF, newF)

Share Button

Python – Parse URLs from files in directory to check if links are up/down before executing yum on Centos.

This script parses the files in the yum repo directory and checks if all URLS are up before proceeding to execute yum. It creates two lists to do a sanity check. One list stores the number of URLs found and the second list stores the number of links that have been found to be working. If those two numbers do not match the execution of YUM is aborted. Please note, this script will work only with custom made yum repos.

import os
import subprocess
import re
import urllib2
import os.path


def check_Software():
    path = '/usr/bin/gcc'
    if os.path.isfile(path):
        pass
    else:
        try:
            subprocess.call(["yum", "install", "-y", "gcc"])
            if not os.patch.isfile(path):
                print "Sending email"

        except OSError as er:
            print er

def yum_link_Search():
    #These two lists are being used as control. The status list is used to append succesful HTTP connections
    #for each of the URLs captured from the files in the YUM directory. The latter is stored in the files_list list.
    #The number (amount of elements) is being compared. A success condition is when each site returns an alive code of
    #200. This tells us that all sites are up and running. View lines between 52 and 60
    status_list = []
    files_list = []
    src_dict = ("/etc/yum.repos.d/") #Specify base directory
    pattern = re.compile ('http\S+') #CPatter to search for

    for yum_files in os.listdir(src_dict): # obtain list of files in directory
        files = os.path.join(src_dict, yum_files) #join the full path with the names of the files.
        files_list.append(yum_files)
        strng = open(files) #We need to open the files
        for lines in strng.readlines(): #We then need to read the files
            if re.search(pattern, lines): #If we find the pattern we are looking for
                print re.split(r'=', lines)[1] #We split using as a delimeter the = sign.
                try:

                    status_link = urllib2.urlopen(re.split(r'=', lines)[1]).getcode()
                    #if  status_link == 200: #Check if the site is up
                    status_list.append(status_link)#View notes starting on line 29 for these conditionals
                    print status_list
                    print files
                    if len(files_list) > len(status_list): #View notes starting on line 29 for these conditionals
                        print "Not all Links are up - ABORTING"
                        exit();
                    if len(files_list) < len(status_list):
                        print "Not all Links are up - ABORTING"
                        exit();
                    elif len(files_list) == len(status_list):
                        print "Will run yum installer" #here we can call other functions since everything seems to be working fine.
                        #call the yum function here
                except StandardError as er:#Here we are capturing errors of other codes.
                    print "Site down"
                    print status_link



yum_link_Search()
Share Button