ABSP, Ch 9, Practice Project #3

My solutions to the Practice Project #3 in Chapter 9 of the excellent book “Automate the Boring Stuff with Python” (ABSP)

#! usr/bin/env python3
# by lorenzo - 21/09/2017
# filling in the gaps - program that finds all files with a given prefix, 
#                       such as spam001.txt, spam002.txt, and so on,
#                       in a single folder, locates any gaps 
#                       in the numbering (such as if there is a spam001.txt and #                       spam003.txt but no spam002.txt) and subsequently
#                       renames all the later files to close this gap.                       

import os, sys, shutil

print('Enter location of folder to search in:')
myFolder = os.path.abspath(input())
if not os.path.exists(myFolder):
    print("This path doesn't exist!")
    sys.exit()

print('Enter name part of prefix of the files:')
fileName = input()

print('From which number should the numbering of the files start?')
nStart = int(input())

print('At which number should the numbering of the files end?')
nStop = int(input())

print('How many digits in numbering? (ex. 010 has three digits)')
nDig = int(input())

listOfFiles = []
for i in range(nStart, nStop + 1):    
    nZero = nDig - len(str(i))    
    currentFile = fileName + nZero * '0' + str(i)
    listOfFiles.append(currentFile)

os.chdir(myFolder)
myList = []
for thisFile in os.listdir():
    if thisFile in listOfFiles:
        myList.append(thisFile)
myList.sort()    
for i in range(len(myList)):
        if listOfFiles[i] != myList[i]:
            shutil.move(myList[i], listOfFiles[i])
#! usr/bin/env python3
# by lorenzo - 21/09/2017
# filling in the gaps (alternative version)
# program that finds all files with a given prefix, such as spam001.txt, 
# spam002.txt, and so on, in a single folder, locates any gaps 
# in the numbering (such as if there is a spam001.txt and spam003.txt 
# but no spam002.txt) and subsequently creates appropriately named files
# to close this gap.
                                   
import os, sys

print('Enter location of folder to search in:')
myFolder = os.path.abspath(input())
if not os.path.exists(myFolder):
    print("This path doesn't exist!")
    sys.exit()

print('Enter name part of prefix of the files:')
fileName = input()

print('From which number should the numbering of the files start?')
nStart = int(input())

print('At which number should the numbering of the files end?')
nStop = int(input())

print('How many digits in numbering? (ex. 010 has three digits)')
nDig = int(input())

listOfFiles = []
for i in range(nStart, nStop + 1):    
    nZero = nDig - len(str(i))    
    currentFile = fileName + nZero * '0' + str(i)
    listOfFiles.append(currentFile)
os.chdir(myFolder)
for myFile in listOfFiles:
    if myFile not in os.listdir():
        thisFile = open(myFile, 'w')
        thisFile.close()
    else:
        continue
#! usr/bin/env python3
# by lorenzo - 21/09/2017
# added challenge - program that can insert gaps into numbered files
#                   so that a new file can be added.

import os, sys, shutil

def Add(myString, numToAdd):
    numZeros = 0
    aNumber = []
    aString = []
    for ch in myString:
        if ch.isdecimal():
            aNumber.append(ch)
        else:
            aString.append(ch)
    
    for i in range(len(aNumber)):
        if aNumber[i] == 0:
            numZeros += 1
            del aNumber[i]
        else:
            break
        
    myNumber = ''.join(aNumber)
    thisNumber = int(myNumber) + numToAdd
    strNumb = str(thisNumber)
    finalList = aString[:]
    zeros = numZeros - (len(str(thisNumber))-len(myNumber))
                       
    for num in range(zeros):
                        finalList.append('0')

    for j in range(len(strNumb)):
                        finalList.append(strNumb[i])

    returnStr = ''.join(finalList)
    return returnStr
                           
print('Enter location of folder to search in:')
myFolder = os.path.abspath(input())
if not os.path.exists(myFolder):
    print("This path doesn't exist!")
    sys.exit()

print('Enter prefix of the files:')
fileName = input()

print('Enter complete name of the file from which you want to add spaces:')
fileStart = input()

print('How many spaces do you want between this file and the next one?')
nSpace = int(input())

os.chdir(myFolder)
myList = []
for thisFile in os.listdir():
    if thisFile.startswith(fileName) and thisFile[-1].isdigit():
        myList.append(thisFile)
myList.sort()
myIndex = myList.index(fileStart)
thisList = myList[myIndex+1:]

for myFile in thisList:
    os.rename(myFile, Add(myFile,nSpace))

Comments about the code are welcome.

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s