python - 如何将文件和文件夹移动到指定目录?

标签 python shutil

有一段代码可以将文件从一个目录移动到另一个目录,但不会移动文件夹。

import os,glob
import shutil

inpath = str5
outpath = str6

os.chdir(inpath)
for file in glob.glob("*.*"):

    shutil.move(inpath+'/'+file,outpath)

如何让它将文件和文件夹都移动到指定目录?

最佳答案

*.* 选择具有扩展名的文件,因此省略子文件夹。

使用*选择文件和文件夹。

然后您应该会看到您想要的结果。

for file in glob.glob("*"):
    shutil.move(inpath+'/'+file,outpath)

关于python - 如何将文件和文件夹移动到指定目录?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/70505242/

相关文章:

python - 定义列表框后更改列表框的绑定(bind)

python - 使用标签 '{{value | first}}' 返回模型中的第一张图像

python 2 [错误 32] 进程无法访问该文件,因为它正被另一个进程使用

python argparse.ArgumentParser 读取配置文件

python - Errno 2 使用 python shutil.py 文件目标没有这样的文件或目录

python - shutil.rmtree 删除只读文件

python - 修复代码以摆脱 ValueError : cannot set using a multi-index selection indexer with a different length

python - 如何简洁地创建一个临时文件,该文件是python中另一个文件的副本

python - python核心使用的是二分查找算法吗?

Python - 根据内容分隔文件