Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/353.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 Shutil.copy2()操作不允许出现错误_Python_Shutil - Fatal编程技术网

Python Shutil.copy2()操作不允许出现错误

Python Shutil.copy2()操作不允许出现错误,python,shutil,Python,Shutil,我创建了下面的基本脚本,它扫描目录中的wav文件,并根据文件属性将它们复制到嵌套目录中 """ Script that copies data from Peersonic RPA3 into directory using file name, date and time """ import datetime as dt import os import shutil import time from pathlib import

我创建了下面的基本脚本,它扫描目录中的wav文件,并根据文件属性将它们复制到嵌套目录中

"""
Script that copies data from Peersonic RPA3 into directory
using file name, date and time
"""

import datetime as dt
import os
import shutil
import time
from pathlib import Path


# Function to check if survey is dusk or dawn
def duskdawn(file):
    filetime = dt.datetime.fromtimestamp(os.path.getmtime(file))
    if file_time.time() > dt.time(12):
        return "Dusk"
    else:
        return "Dawn"


def file_date(file):
    return time.strftime('%d.%m.%Y', time.localtime(os.path.getmtime(file)))


def client(file):
    return file.name[0:3]

client_lst = ['IES', 'DEC', 'TEP']

# Directory of files
path = "/Volumes/BATrecorder/WAVFILES.DIR"
for entry in os.scandir(path):
    source = entry.path
    perm = os.stat(entry).st_mode
    client_name = (client(entry))
    if entry.name.endswith(".WAV"):
        if client_name in client_lst:
            client_name = client_name
        else:
            client_name = "unknown"
    destination = f"/Volumes/Seagate Exp/Work 2020/Bat recordings/{client_name}/{file_date(entry)}/{duskdawn(entry)}"
    Path(destination).mkdir(parents=True, exist_ok=True)
    shutil.copy2(source, destination)
最初我使用了标准的
shutil.copy()
函数,效果很好。但是,我需要保留原始文件元数据,因此需要使用
shutil.copy2()
。当我使用此选项时,会出现以下错误:

Traceback (most recent call last):
  File "/Volumes/Seagate Exp/Work 2020/file_copy.py", line 43, in <module>
    shutil.copy2(source, destination)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 258, in copy2
    copystat(src, dst, follow_symlinks=follow_symlinks)
  File "/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py", line 218, in copystat
    lookup("chflags")(dst, st.st_flags, follow_symlinks=follow)
PermissionError: [Errno 1] Operation not permitted: '/Volumes/Seagate Exp/Work 2020/Bat recordings/unknown/31.12.2014/Dusk/@AAA_000.WAV'
回溯(最近一次呼叫最后一次):
文件“/Volumes/segate Exp/Work 2020/File_copy.py”,第43行,在
shutil.copy2(源、目标)
copy2中的文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py”,第258行
copystat(src、dst、follow_symlinks=follow_symlinks)
copystat中的文件“/Library/Frameworks/Python.framework/Versions/3.6/lib/python3.6/shutil.py”,第218行
查找(“chflags”)(dst、st.st_标志、follow_符号链接=follow)
PermissionError:[Errno 1]不允许操作:'/Volumes/segate Exp/Work 2020/Bat recordings/unknown/31.12.2014/dash//AAA_000.WAV'

这里发生了什么导致此问题?

您似乎没有在目标中写入的权限。您正在将整个目标创建为目录,然后尝试将您的文件复制为您刚才创建的目录,而不是此目录下的文件。您可能需要类似于
Path(os.Path.dirname(destination)).mkdir(…)
instead的内容。如果您尝试此操作,我发现奇怪的是,它会写入初始文件,然后返回一个错误