Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 将文件从src文件夹移动到dest文件夹并覆盖现有文件_Python 3.x - Fatal编程技术网

Python 3.x 将文件从src文件夹移动到dest文件夹并覆盖现有文件

Python 3.x 将文件从src文件夹移动到dest文件夹并覆盖现有文件,python-3.x,Python 3.x,我对python很陌生。我想写一个脚本,可以将文件从src文件夹移动到dest文件夹。如果文件已存在于dest文件夹中,则文件仍会从src移动到dest,并覆盖现有文件。我现在唯一能想到的就是检查dest文件夹中是否有现有文件,如果有,那么在将文件从src移动到dest之前,该文件将被删除。这是我到目前为止所拥有的 src_file=os.listdir(src) for f in src_file: fullname=os.path.join(dest,f) if o

我对python很陌生。我想写一个脚本,可以将文件从src文件夹移动到dest文件夹。如果文件已存在于dest文件夹中,则文件仍会从src移动到dest,并覆盖现有文件。我现在唯一能想到的就是检查dest文件夹中是否有现有文件,如果有,那么在将文件从src移动到dest之前,该文件将被删除。这是我到目前为止所拥有的

src_file=os.listdir(src)

for f in src_file: 
    fullname=os.path.join(dest,f)   
    if os.path.exists(fullname):
        os.remove(fullname)
        shutil.move(src,dest)
这个脚本的问题是,src中的文件将被移动到src中的一个文件夹中,src文件夹将被删除。有更好的方法吗

编辑:我有很多数据(百万文件)流到我的src文件夹。所以我需要将文件从src移动到dest。在我的情况下,shutil.copy不起作用,因为我最终会有一个大文件夹
谢谢

只需使用
shutil.copy(src,dest)
shutil.copy对我来说不是很好的解决方案,因为我有数百万个文件流式传输到我的src文件夹。所以我想将文件移出src文件夹以减小大小。这是否回答了您的问题?