简体   繁体   English

如何将目录中子文件夹中的文件移动到python中的另一个目录?

[英]How do I move files within a subfolder in a directory to another directory in python?

import os
import shutil
source_dir = r'C:\\Users\\Andre\\Downloads'
image_dir = r'C:\\Users\\Andre\\Downloads\\images'

file_names = os.listdir(source_dir)
    
for file_name in file_names:
    if '.png' in file_name:
        shutil.move(os.path.join(source_dir, file_name), image_dir)

This current code will move all pngs from source dir to image dir, how can I make it so it also includes pngs nested in another subdirectory inside the source dir?此当前代码会将所有pngs从源目录移动到图像目录,我如何才能使其还包括嵌套在源目录内另一个子目录中的pngs for example C:\\Users\\Andre\\Downloads\\pictures例如C:\\Users\\Andre\\Downloads\\pictures

You can break the moving functionality into a function, then call that function on each directory.您可以将移动功能分解为一个函数,然后在每个目录上调用该函数。

def move_pngs(src, dst):
    for file_name in os.listdir(src):
        if file_name.endswith('.png'):
            shutil.move(os.path.join(src, file_name), dst)

move_pngs(source_dir, image_dir)
move_pngs(os.path.join(source_dir, 'pictures'), image_dir)

... Or maybe go full recursive. ...或者也许完全递归。

def move_pngs(src, dst):
    for file_name in os.listdir(src):
        fullname = os.path.join(src, file_name)
        if os.path.isdir(fullname) and fullname != dst:
            move_pngs(fullname, dst)
        elif file_name.endswith('.png'):
            shutil.move(fullname), dst)

This will visit all subdirectories recursively, and move all files into one flat directory, meaning if there are files with the same names anywhere, generally the one from a deeper subdirectory will win, and the others will be overwritten.这将递归访问所有子目录,并将所有文件移动到一个平面目录中,这意味着如果任何地方都有同名的文件,通常来自更深子目录的一个将获胜,而其他的将被覆盖。

You can simply just change the source_dir to also include the subdirectory which you want to move the pngs from and run then the loop again.您可以简单地更改source_dir以包含要从中移动pngs的子目录,然后再次运行循环。
In this case:在这种情况下:

subdirectory = '\pictures'
source_dir += subdirectory 

file_names = os.listdir(source_dir)    
for file_name in file_names:
    if '.png' in file_name:
        shutil.move(os.path.join(source_dir, file_name), image_dir)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何读取目录中的文件,然后出来读取下一个目录 python 中的文件? - How do I read files within directory and then come out and read files within next directory python? 如何在目录中移动文件而不是文件夹? - How do you move files, but not folders, within a directory? 如何使用python链接到另一个目录? - How do I link to another directory with python? 如何使用Python将文件从一个子文件夹处理到每个目录中的另一个子文件夹? - How to process files from one subfolder to another in each directory using Python? 如何将文件和文件夹移动到指定目录? - How do I move both files and folders to the specified directory? 如何将文件从一个目录移动到另一个目录? - How to move files from a directory to another? 如何在另一个文件目录中的另一个脚本中导入Python文件? - How do I import a Python file within another script which is in another file directory? 如何使用 python 将 email 从一个目录移动到另一个目录 - How can I move an email from a directory to another using python 如何将某个目录中所有文件的一定百分比移动到另一个目录中? - How to move a a certain percentage of all the files in a directory into another directory? 如何将 Python 3.6 安装移动到不同的目录? - How do I move a Python 3.6 installation a different directory?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM