I would like to know how to use a list contain with file names returned from a function to search a directory based on that list then copy the matching files to another directory. Following is what I would like to accomplish in logical order.

1. Existing Function getPromptList() return PromptList which contain a list of files.
2. Create a Function copyLiveToPrompt which taking the PromptList against the Source Directory and find files match the PromptList then copy them to the Destination Directory.

New to Python and will post some code. Please help... Thanks.

Post code and ask specific questions, read forum rules and/or pytony's signature.

Post code and ask specific questions, read forum rules and/or pytony's signature.

Yes, show some effort. (Here you can click in my version of 'Daniweb for dummies' from my signature, if you wish)

Following is code of the functions that I would like to accomplish for this task.
1. Existing Function getPromptList() return PromptList which contain a list of files.
2. Create a Function copyLiveToPrompt which taking the PromptList against the Source Directory and find files match the PromptList then copy them to the Destination Directory.

import os
import sys
import time
import zipfile
import fnmatch
from logalert import log
......................
def getPromptList():
#promptList is a list in the global scope that will contain the name of each file #that the CSV file says to install
    global promptList
    promptList = []
#------------------------------------------------------------------------------
#This looks at the first item in each of the lists in csvContents, and appends
#the fourth item (name of the files to be installed) to promptList if
#the first item in the list is 'add'
    try:
        for x in range(0,len(csvContents)):
            if csvContents[x][0] == 'add':
                promptList.append(csvContents[x][3] + '.vox')
        return
    except Exception as error:
        log(logFile, 'An error has occurred getPromptList function: ' + str(error))
        raise

def copyLiveToPrompt():
#This function will copy all of the appropriate files based on promptList returned #from getPromptList() function from livePromptDir directory to promptDir directory
	try:

		except Exception as error:
		log(logFile, 'An error has occurred in the copyLiveToPrompt function: ' + str(error))
		raise
import os      # Not used
import sys     # Not used.
import time    # Not used.
import zipfile # hint from teacher to use this? Not used.
import fnmatch # hint from teacher to use this? Not used.
from logalert import log # ?? Python has not standard module called logalert
......................
def getPromptList():
# NO THE prompt list should be local and returned, like your task description said
#promptList is a list in the global scope that will contain the name of each file 
#that the CSV file says to install
    #global promptList
    promptList = []
#------------------------------------------------------------------------------
#This looks at the first item in each of the lists in csvContents, and appends
#the fourth item (name of the files to be installed) to promptList if
#the first item in the list is 'add'
    try:
        for x in range(0,len(csvContents)):
            if csvContents[x][0] == 'add':
                promptList.append(csvContents[x][3] + '.vox')
        # should we really return None?
        return
    except Exception as error:
        log(logFile, 'An error has occurred getPromptList function: ' + str(error))
        raise

def copyLiveToPrompt():
#This function will copy all of the appropriate files based on promptList returned #from getPromptList() function from livePromptDir directory to promptDir directory
	try:

		except Exception as error: ## indention error
		log(logFile, 'An error has occurred in the copyLiveToPrompt function: ' + str(error))
		raise

(Naming is not according to Python convention of small_with_underlines for variables and Capitalized_for_objects)

The getPromptlist() did returnd. The question is how you use the returned promptList in the function copyLiveToPrompt()? Like the desctiptions stated "#This function will copy all of the appropriate files based on promptList returned from getPromptList() function from livePromptDir directory to promptDir directory". The goal is to copy all files by search file names in promptList match file names in the Source directory to Destination directory. I have defined the Source Dir = liveromptDir and the Destination Dir = promptDir. Can Python do something like this:

os.chdir(livepromptDir)
for file in os.listdir(livepromptDir)
      if file match promptList 
      copy each file from livepromptDir to promptDir

I do appreciate any help.

You must show effort, start by reading fnmatch documentation. And do not bump your thread with empty of meaning posts, please.

OK. This is what I did. Does fnmatch can take the List(promptList) as pattern in fnmatch.filter()? Consider the following code.

import os
import sys
import time
import zipfile
import fnmatch
import shutilpromptList
from logalert import log

def getPromptList():
#promptList is a list in the global scope that will contain the name of each Voice Prompt that the
#CSV file says to install
    global promptList
    promptList = []
#------------------------------------------------------------------------------
#This looks at the first item in each of the lists in csvContents, and appends
#the fourth item (name of the Voice Prompt to be installed) to promptList if
#the first item in the list is 'add'
    try:
        for x in range(0,len(csvContents)):
            if csvContents[x][0] == 'add':
                promptList.append(csvContents[x][3] + '.vox')
        return
    except Exception as error:
        log(logFile, 'An error has occurred getPromptList function: ' + str(error))
        raise


def copyLiveToPrompt():
#This function will copy all of the appropriate Voice Prompt files from LivePrompts directory 
#to promptDir based on promptList
	try:
		for filename in fnmatch.filter(os.listdir(livepromptDir), 'promptList'):
			shutil.copy(filename, promptDir % filename)
			return
	except Exception as error:
		log(logFile, 'An error has occurred in the copyLiveToPrompt function: ' + str(error))
		raise

Thanks for your time!

Be a part of the DaniWeb community

We're a friendly, industry-focused community of developers, IT pros, digital marketers, and technology enthusiasts meeting, networking, learning, and sharing knowledge.