- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我正在使用 python 中的 shutil.copy
方法。
我找到了下面列出的定义:
def copyFile(src, dest):
try:
shutil.copy(src, dest)
# eg. src and dest are the same file
except shutil.Error as e:
print('Error: %s' % e)
# eg. source or destination doesn't exist
except IOError as e:
print('Error: %s' % e.strerror)
我正在循环中访问定义。该循环基于每次都会更改的字符串。该代码查看目录中的所有文件,如果它在文件中看到字符串的一部分,则将其复制到新位置
我很确定会有重复的文件。所以我想知道会发生什么。
它们会被复制,还是会失败?
最佳答案
shutil.copy
不会将文件复制到新位置,它会覆盖文件。
Copy the file src to the file or directory dst. If dst is a directory, a file with the same basename as src is created (or overwritten) in the directory specified. Permission bits are copied. src and dst are path names given as strings.
因此您必须自行检查目标文件是否存在并适本地更改目标。例如,这是您可以用来实现安全复制的方法:
def safe_copy(file_path, out_dir, dst = None):
"""Safely copy a file to the specified directory. If a file with the same name already
exists, the copied file name is altered to preserve both.
:param str file_path: Path to the file to copy.
:param str out_dir: Directory to copy the file into.
:param str dst: New name for the copied file. If None, use the name of the original
file.
"""
name = dst or os.path.basename(file_path)
if not os.path.exists(os.path.join(out_dir, name)):
shutil.copy(file_path, os.path.join(out_dir, name))
else:
base, extension = os.path.splitext(name)
i = 1
while os.path.exists(os.path.join(out_dir, '{}_{}{}'.format(base, i, extension))):
i += 1
shutil.copy(file_path, os.path.join(out_dir, '{}_{}{}'.format(base, i, extension)))
此处,'_number'
被插入到扩展名之前,以在重复的情况下生成唯一的目的地名称。像 'foo_1.txt'
。
关于Python Shutil.copy 如果我有重复文件,它会复制到新位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/33282647/
shutil.move(src, dest) 方法是否调用 os.system() 方法或 bash 命令“mv”和“cp”? P.S : 我在网上搜索但找不到任何有用的信息。 最佳答案 使用来源卢克
我正在构建一个相对简单的应用程序,它请求目录,检查它们是否正确,然后删除其中一个并使用另一个目录的内容重新创建它。我遇到了这种奇怪的行为,我会尝试解释: 当我打开目标文件夹窗口并且它是空的时,出现
我正在尝试在Python中使用shutil模块。我想将文件夹 ('movee') 从我的 cwd 复制到 'D' 驱动器。代码如下: import shutil shutil.copytree('mo
我有一个很大的图像数据集(大约 3000 个文件)。我的问题很简单,我想将随机选择的图像文件复制到另一个目的地。我使用 random.sample 选择五百张图片并将它们的名称存储在列表中。我现在想将
昨天,根据一位海报的出色建议,我开始使用 shutil.copyfileobj 方法制作文件副本。 我的程序应该制作文件的精确副本,删除最后一个字节并保存新副本。 我昨晚用一些非常小的 ASCII 文
我有一个文件夹,里面有照片,我想将照片从那个文件夹移动到另一个文件夹。文件: IMAG_01.JPG IMAG_02.JPG IMAG_03.JPG IMAG_04.JPG IMAG_05.JPG I
我正在尝试对shutil.copyfileobj()函数进行猴子修补,以便将其默认长度从16*1024更改为更大的值(128*1024)。在内部,其他 Shutil 方法(如 move)调用 copy
我想移动很多文件。这些文件的路径存储在列表中。我想保留整个目录结构,但想将它们移动到不同的文件夹。 例如文件是D:\test\test1\test1.txtD:\test\test1\test2.tx
尝试处理我的第一个脚本的新 Python 开发人员。目标是编写一个应用程序,通过根据文件类型将文件放入子文件夹来清理我的下载文件夹。 这是我的代码: import shutil import os d
我正在尝试编写一个简单的 python 脚本来移动文件,因为它们进入 ubuntu 中的文件夹。 我正在将文件从我的本地机器移动到我的网络 ubuntu 机器。 请参见下面的代码: f
编程的新手,使用 Python 和“艰难地学习 Python”深入研究它。 出于某种原因,我无法使用 shutil.copy 来复制文件。在我的桌面上,我目前有一个文件“test1.txt”和另一个“
https://docs.python.org/3/library/shutil.html#shutil.move说 If the destination is on the current file
根据http://docs.python.org/library/shutil.html使用这些方法不会复制元数据,包括所有权、ACL 和资源分支。使用基本文件读取方法打开文件并将其逐行写回重复文件是
整个晚上都在为此奋斗。三天前还可以工作,现在我再也无法让它工作了。 当我尝试使用这个时 def unzip_file(archiveFile, finalFolder, archiveFolder)
[编辑:请参阅下面的最终代码]我使用下面的代码从源目录中随机选择 5 个文件,然后将它们复制到新目录。它给了我一个 IO 错误,其中显示“不存在这样的文件或目录‘x’”,其中“x”是不带目录路径的文件
我尝试在析构函数中删除创建的目录: shutil.rmtree("C:\\projects\\project_alpha\\tmp") 它不适用于我的 python 脚本,但是当我通过 python
我想使用函数 shutil.copytree 从我的计算机上的本地服务器复制文件夹,我使用的是 macOS,但今天我遇到了问题,python 总是向我显示相同的消息,“[错误 1] 操作不允许”,但是
我正在尝试使用 python 删除目录,但我不想在此过程中递归删除整个目录路径:即 /home/dir/dir/dirtoberemoved 所以我不想删除更高级别的任何内容,只是一个目录及其所有内容
我正在使用 shutil.copy 将一个文件的内容复制到另一个文件。然而,它导致我的原始文件被删除,并出现“文件中没有数据”的错误 我第一次尝试这个 import shutil sh
我有一个代码,用于将所有 jpg 文件从源移动到目标。代码第一次运行良好,它移动了文件,但如果我再次运行它,它会给出一个文件已存在的错误。 Traceback (most recent call la
我是一名优秀的程序员,十分优秀!