如何根据Python中文件名列表中的项目从目录复制文件
我是Python新手,我创建了以下函数,根据列表中的项目(PrompList)从目录(livepromptDir)复制文件。到目前为止,它只将列表中的第一项复制到目标目录。请帮忙!提前谢谢如何根据Python中文件名列表中的项目从目录复制文件,python,Python,我是Python新手,我创建了以下函数,根据列表中的项目(PrompList)从目录(livepromptDir)复制文件。到目前为止,它只将列表中的第一项复制到目标目录。请帮忙!提前谢谢 def copyItemToPrompt(): #This function will copy all of the appropriate Voice Prompt files from LivePrompts directory to promptDir based on promptList
def copyItemToPrompt():
#This function will copy all of the appropriate Voice Prompt files from LivePrompts directory to promptDir based on promptList
os.chdir(livepromptDir)
try:
for i in range(0,len(promptList)):
for filename in fnmatch.filter(os.listdir(livepromptDir), promptList[i]):
shutil.copy(filename, promptDir)
return
except Exception as error:
log(logFile, 'An error has occurred in the copyLiveToPrompt function: ' + str(error))
raise
您希望将
return
移动到for
循环之外,否则函数将在第一次迭代后返回。实际上,您甚至不需要退货:
def copyItemToPrompt():
"""This function will copy all of the appropriate Voice Prompt files from LivePrompts directory to promptDir based on promptList"""
os.chdir(livepromptDir)
try:
for i in range(0,len(promptList)):
for filename in fnmatch.filter(os.listdir(livepromptDir), promptList[i]):
shutil.copy(filename, promptDir)
except Exception as error:
log(logFile, 'An error has occurred in the copyLiveToPrompt function: ' + str(error))
raise
您希望将
return
移动到for
循环之外,否则函数将在第一次迭代后返回。实际上,您甚至不需要退货:
def copyItemToPrompt():
"""This function will copy all of the appropriate Voice Prompt files from LivePrompts directory to promptDir based on promptList"""
os.chdir(livepromptDir)
try:
for i in range(0,len(promptList)):
for filename in fnmatch.filter(os.listdir(livepromptDir), promptList[i]):
shutil.copy(filename, promptDir)
except Exception as error:
log(logFile, 'An error has occurred in the copyLiveToPrompt function: ' + str(error))
raise
正如@rcriii所提到的,返回是使函数短路的原因。我不确定您想要完成什么,但我认为您只想在给定glob模式列表的情况下,将文件列表从一个dir复制到另一个dir 如果是这样的话,假设你有这样一个目录:
.
├── a
│ ├── file1
│ ├── file2
│ └── tmp3
└── b
copy_with_patterns('a', 'b', ['file*'])
.
├── a
│ ├── file1
│ ├── file2
│ └── tmp3
└── b
├── file1
└── file2
这个函数应该为您提供一种更简洁的方法来实现这一点(像for i in range…
这样的东西通常不会像您在这里使用的那样使用)。此外,如果您无法更改回去,更改目录有时会给您带来问题
import shutil
from itertools import chain
from os import path
from glob import glob
def copy_with_patterns(src, dest, patterns):
# add src dir to given patterns
patterns = (path.join(src, x) for x in patterns)
# get filtered list of files
files = set(chain.from_iterable(glob(x) for x in patterns))
# copy files
for filename in files:
shutil.copy(filename, filename.replace(src, dest))
按如下方式调用此函数:
.
├── a
│ ├── file1
│ ├── file2
│ └── tmp3
└── b
copy_with_patterns('a', 'b', ['file*'])
.
├── a
│ ├── file1
│ ├── file2
│ └── tmp3
└── b
├── file1
└── file2
将使您的目录现在如下所示:
.
├── a
│ ├── file1
│ ├── file2
│ └── tmp3
└── b
copy_with_patterns('a', 'b', ['file*'])
.
├── a
│ ├── file1
│ ├── file2
│ └── tmp3
└── b
├── file1
└── file2
正如@rcriii所提到的,返回是使函数短路的原因。我不确定您想要完成什么,但我认为您只想在给定glob模式列表的情况下,将文件列表从一个dir复制到另一个dir 如果是这样的话,假设你有这样一个目录:
.
├── a
│ ├── file1
│ ├── file2
│ └── tmp3
└── b
copy_with_patterns('a', 'b', ['file*'])
.
├── a
│ ├── file1
│ ├── file2
│ └── tmp3
└── b
├── file1
└── file2
这个函数应该为您提供一种更简洁的方法来实现这一点(像for i in range…
这样的东西通常不会像您在这里使用的那样使用)。此外,如果您无法更改回去,更改目录有时会给您带来问题
import shutil
from itertools import chain
from os import path
from glob import glob
def copy_with_patterns(src, dest, patterns):
# add src dir to given patterns
patterns = (path.join(src, x) for x in patterns)
# get filtered list of files
files = set(chain.from_iterable(glob(x) for x in patterns))
# copy files
for filename in files:
shutil.copy(filename, filename.replace(src, dest))
按如下方式调用此函数:
.
├── a
│ ├── file1
│ ├── file2
│ └── tmp3
└── b
copy_with_patterns('a', 'b', ['file*'])
.
├── a
│ ├── file1
│ ├── file2
│ └── tmp3
└── b
├── file1
└── file2
将使您的目录现在如下所示:
.
├── a
│ ├── file1
│ ├── file2
│ └── tmp3
└── b
copy_with_patterns('a', 'b', ['file*'])
.
├── a
│ ├── file1
│ ├── file2
│ └── tmp3
└── b
├── file1
└── file2
将您的退换货退换货退换货退换货退换货退换货退换货退换货非常感谢您的帮助。非常感谢您的帮助。