可能不是最有效的,但有效:
find /path-to-files-root/ -type f -name '*.txt' -exec mv -i {} /new-path-to-files/txt/ \;
然后我跑了:
def get_all_files(path, pattern):
#see https://stackoverflow.com/questions/17282887/getting-files-with-same-name-irrespective-of-their-extension
datafiles = []
for root,dirs,files in os.walk(path):
for file in fnmatch.filter(files, pattern):
datafiles.append(file)
return datafiles
txt_files = [f for f in os.listdir(txt_path) if isfile(join(txt_path, f))]
doc_files = [f for f in os.listdir(doc_path) if isfile(join(doc_path, f))]
for i, txt_file in enumerate(txt_files):
filename = (os.path.splitext(txt_file)[0])
doc_files = get_all_files(doc_path, '{0}.doc'.format(filename))
if len(doc_files)== 1:
doc_file = doc_files[0]
#do something with txt_file and doc_file