[英]How to move folder in a path to another path in windows using python3?
I need a script to move all contents from source folder and create/replace into destination folder without removing any existing files/folders in destination.我需要一个脚本来移动源文件夹中的所有内容并创建/替换到目标文件夹中,而不删除目标中的任何现有文件/文件夹。 The contents of test1 folder will be a html file along with some images in png format.
test1 文件夹的内容将是一个 html 文件以及一些 png 格式的图像。
source path : C:\\Users\\USERNAME\\Documents\\LocalFolder\\reports\\v2源路径:C:\\Users\\USERNAME\\Documents\\LocalFolder\\reports\\v2
source folder Name : test1源文件夹名称: test1
Destination path : T:\\remoteReports\\myTests Path to be created while copying: \\LocalFolder\\reports\\v2目标路径:T:\\remoteReports\\myTests复制时创建的路径: \\LocalFolder\\reports\\v2
Folder to copy from source into destination : test1从源复制到目标的文件夹: test1
final path in destinaton: T:\\remoteReports\\myTests\\LocalFolder\\reports\\v2\\test1 destinaton 中的最终路径: T:\\remoteReports\\myTests\\LocalFolder\\reports\\v2\\test1
I have taken a piece of code from stack overflow and using it for my application but running the script is not creating the path and folder in the destination.我从堆栈溢出中提取了一段代码并将其用于我的应用程序,但运行脚本并没有在目标中创建路径和文件夹。
import time
import os
import random
import shutil
import pathlib
def move_files(root_src_dir,root_dst_dir):
print(root_src_dir)
print(root_dst_dir)
for src_dir, dirs, files in os.walk(root_src_dir):
dst_dir = src_dir.replace(root_src_dir,root_dst_dir, 1)
if not os.path.exists(dst_dir):
os.makedirs(dst_dir)
for file_ in files:
src_file = os.path.join(src_dir, file_)
dst_file = os.path.join(dst_dir, file_)
if os.path.exists(dst_file):
# in case of the src and dst are the same file
if os.path.samefile(src_file, dst_file):
continue
os.remove(dst_file)
shutil.move(src_file, dst_dir)
srcPath = os.path.join("C:", "Users", os.getlogin(),"Documents", "LocalFolder", "v2", "test1")
dstPath = os.path.join("T:", "remoteReports", "myTests", "LocalFolder", "v2", "test1")
move_files(srcPath,dstPath )
It will be helpful if someone can guide me with this !如果有人可以指导我,这将很有帮助!
You can use pathlib
to copy all files from one folder to another:您可以使用
pathlib
将所有文件从一个文件夹复制到另一个文件夹:
from pathlib import Path
from shutil import copy
src = Path(r"C:\Users\USERNAME\Documents\LocalFolder\reports\v2")
dst = Path(r"T:\remoteReports\myTests\LocalFolder\reports\v2")
for file in src.iterdir():
if file.is_file() and not (dst / file.name).is_file():
copy(file, dst)
If you want to copy whole tree including sub directories and it's content, you can apply os.walk()
:如果要复制整个树,包括子目录及其内容,可以应用
os.walk()
:
from pathlib import Path
from os import walk
from shutil import copy
src = Path(r"C:\Users\USERNAME\Documents\LocalFolder\reports\v2")
dst = Path(r"T:\remoteReports\myTests\LocalFolder\reports\v2")
for dir_name, _, filenames in walk(src):
src_path = Path(dir_name)
dst_path = dst / src_path.relative_to(src)
if not dst_path.is_dir():
dst_path.mkdir()
for file in filenames:
dst_file = dst_path / file
if not dst_file.is_file():
copy(src_path / file, dst_path)
Or you can make function from code I've suggested firstly and use it recursively:或者您可以从我首先建议的代码中创建函数并递归使用它:
from pathlib import Path
from shutil import copy
def copy_folder(src_path, dst_path):
if not dst_path.is_dir():
dst_path.mkdir()
for file in src_path.iterdir():
if file.is_dir():
copy_folder(file, dst_path / file.relative_to(src_path)) # recursion
elif file.is_file() and not (dst_path / file.name).is_file():
copy(file, dst_path)
src = Path(r"C:\Users\USERNAME\Documents\LocalFolder\reports\v2")
dst = Path(r"T:\remoteReports\myTests\LocalFolder\reports\v2")
copy_folder(src, dst)
BUT You don't really need to implement all this manually, because shutil.copytree()
already exists and does exactly the same that all options I've provided before.但是你真的不需要手动实现所有这些,因为
shutil.copytree()
已经存在并且与我之前提供的所有选项完全相同。 Important notice: you should pass dirs_exist_ok
argument to not throw an exception if directory in destination path already exists.重要提示:如果目标路径中的目录已经存在,您应该传递
dirs_exist_ok
参数以不引发异常。
from shutil import copy, copytree
from os.path import exists
src = r"C:\Users\USERNAME\Documents\LocalFolder\reports\v2"
dst = r"T:\remoteReports\myTests\LocalFolder\reports\v2"
copytree(src, dst, copy_function=lambda s, d: not exists(d) and copy(s, d), dirs_exist_ok=True)
Here I've used lambda
which check does file already exist to not overwrite it, you can omit this parameter if you don't care that files will be overwritten.在这里,我使用了
lambda
来检查文件是否已经存在以不覆盖它,如果您不关心文件会被覆盖,则可以省略此参数。
If you want to move all files, you can passshutil.move()
to copy_function
:如果你想移动所有文件,你可以将
shutil.move()
传递给copy_function
:
from shutil import move, copytree
...
copytree(src, dst, copy_function=move, dirs_exist_ok=True)
It'll move all files, but leave tree directory.它将移动所有文件,但离开树目录。 If you want to copy all directory tree and delete it from source, you can call
shutil.copytree()
and shutil.rmtree()
:如果要复制所有目录树并从源中删除它,可以调用
shutil.copytree()
和shutil.rmtree()
:
from shutil import copytree, rmtree
...
copytree(src, dst, dirs_exist_ok=True)
rmtree(src)
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.