Find centralized, trusted content and collaborate around the technologies you use most.
Teams
Q&A for work
Connect and share knowledge within a single location that is structured and easy to search.
标题解释了我所追求的。请注意,子目录不会包含任何目录,只有 文件(*.JPG)。本质上,只是将文件树中的所有内容上移一级。
例如,从 ~/someDir/folder1/*, ~/someDir/folder2/*, ... , ~/someDir/folderN/*. 我希望将子目录的所有内容提升到~/someDir/.
~/someDir/folder1/*
~/someDir/folder2/*
~/someDir/folderN/*
~/someDir/
shutil.move 是移动文件的好选择。
import shutil import os source = "/parent/subdir" destination = "/parent/" files_list = os.listdir(source) for files in files_list: shutil.move(files, destination)
对于递归移动,您可以尝试shutil.copytree(SOURCE, DESTINATION). 它只是复制所有文件,如果需要,您可以手动清理源目录。
shutil.copytree(SOURCE, DESTINATION)