使用 python cp -r from_dir/* to_dir
有没有一种简单的方法可以用 python 模拟命令 cp -r from_dir/* to_dir ? shutil.copytree
不适合,因为 to_dir
存在。
Is there an easy way to emulate the command cp -r from_dir/* to_dir
with python? shutil.copytree
is not suitable because to_dir
exists.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
查看
shutil.copytree
的源代码,对其进行修改并使用:Have a look at the source code of
shutil.copytree
, adapt it and use:您只需要使用正确的名称(或相同的名称)来
copytree
you just need to
copytree
with the correct name (or same name)有时,自己直接用 Python 完成所有事情是很好的;话又说回来,直接调用您知道如何控制并知道如何工作的命令通常会更好。
当需求发生变化时,我会毫不犹豫地重写这个
if,但在那之前,它是简短且可读的—— 更多的时间最好花在更大的问题上。它们如何改变的一个很好的例子是报告错误:你没有对此说什么,但一旦需要,我就不会解析 cp 的输出。Sometimes it's nice to do everything directly in Python yourself; then again, it's often nicer to just call the command that you know how to control and know works.
I'd not hesitate to rewrite this
ifwhen requirements change, but until then, it's short and readable — more time is better spent on bigger problems. A good example of how they might change is reporting an error: you've not said anything about that, but I wouldn't be parsing cp's output once that's required.