使用Python将所有文件从一个目录移动到另一个目录 [英] Moving all files from one directory to another using Python

查看:857
本文介绍了使用Python将所有文件从一个目录移动到另一个目录的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想使用Python将所有文本文件从一个文件夹移动到另一个文件夹.我找到了以下代码:

I want to move all text files from one folder to another folder using Python. I found this code:

import os, shutil, glob

dst = '/path/to/dir/Caches/com.apple.Safari/WebKitCache/Version\ 4/Blobs '
try:
    os.makedirs(/path/to/dir/Tumblr/Uploads) # create destination directory, if needed (similar to mkdir -p)
except OSError:
    # The directory already existed, nothing to do
    pass

for txt_file in glob.iglob('*.txt'):
    shutil.copy2(txt_file, dst)

我希望它移动Blob文件夹中的所有文件.我没有收到错误,但也没有移动文件.

I would want it to move all the files in the Blob folder. I am not getting an error, but it is also not moving the files.

推荐答案

尝试一下:

import shutil
import os

source = '/path/to/source_folder'
dest1 = '/path/to/dest_folder'

files = os.listdir(source)

for f in files:
    shutil.move(source+f, dest1)

这篇关于使用Python将所有文件从一个目录移动到另一个目录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆