如何将文件复制到 Python 脚本中的特定文件夹?
How to copy a file to a specific folder in a Python script?
我有一个文件的路径存储在一个变量(比方说)filePath 中。我想将该特定文件复制到 Python 脚本中的另一个特定文件夹。
我试过了
folderPath = (os.getcwd() + "/folder_name/") #to get the path of the folder
shutil.copyfile(filePath, folderPath)
但是我得到一个错误 IOError: [Errno 21] Is a directory
。
我该如何解决这个问题?
我的问题似乎与 如何在 python 中复制文件?
。但实际上,我想将文件复制到 folder/directory,而该问题的大多数答案都提到将一个文件复制到另一个 文件。
folderpath
必须是文件,不是目录。错误说明了一切。做类似的事情:
shutil.copyfile(filePath, folderPath+'/file_copy.extension')
更改您的代码如下:
folderPath = os.path.join('folder_name', os.path.basename(filePath))
shutil.copyfile(filePath, folderPath)
使用 shutil.copy(filePath, folderPath)
而不是 shutil.copyfile()
。这将允许您指定一个文件夹作为目标并复制包含权限的文件。
shutil.copy(src, dst, *, follow_symlinks=True)
:
Copies the file src to the file or directory dst. src and dst should be strings. If dst specifies a directory, the file will be copied into dst using the base filename from src. Returns the path to the newly created file.
...
copy() copies the file data and the file’s permission mode (see os.chmod()). Other metadata, like the file’s creation and modification times, is not preserved. To preserve all file metadata from the original, use copy2() instead.
https://docs.python.org/3/library/shutil.html#shutil.copy
参见 shutil.copyfile()
本身中记录的复制差异:
shutil.copyfile(src, dst, *, follow_symlinks=True)
:
Copy the contents (no metadata) of the file named src to a file named dst and return dst. src and dst are path names given as strings. dst must be the complete target file name; look at shutil.copy() for a copy that accepts a target directory path. If src and dst specify the same file, SameFileError is raised.
https://docs.python.org/3/library/shutil.html#shutil.copyfile
我有一个文件的路径存储在一个变量(比方说)filePath 中。我想将该特定文件复制到 Python 脚本中的另一个特定文件夹。
我试过了
folderPath = (os.getcwd() + "/folder_name/") #to get the path of the folder
shutil.copyfile(filePath, folderPath)
但是我得到一个错误 IOError: [Errno 21] Is a directory
。
我该如何解决这个问题?
我的问题似乎与 如何在 python 中复制文件? 。但实际上,我想将文件复制到 folder/directory,而该问题的大多数答案都提到将一个文件复制到另一个 文件。
folderpath
必须是文件,不是目录。错误说明了一切。做类似的事情:
shutil.copyfile(filePath, folderPath+'/file_copy.extension')
更改您的代码如下:
folderPath = os.path.join('folder_name', os.path.basename(filePath))
shutil.copyfile(filePath, folderPath)
使用 shutil.copy(filePath, folderPath)
而不是 shutil.copyfile()
。这将允许您指定一个文件夹作为目标并复制包含权限的文件。
shutil.copy(src, dst, *, follow_symlinks=True)
:Copies the file src to the file or directory dst. src and dst should be strings. If dst specifies a directory, the file will be copied into dst using the base filename from src. Returns the path to the newly created file.
...
copy() copies the file data and the file’s permission mode (see os.chmod()). Other metadata, like the file’s creation and modification times, is not preserved. To preserve all file metadata from the original, use copy2() instead.
https://docs.python.org/3/library/shutil.html#shutil.copy
参见 shutil.copyfile()
本身中记录的复制差异:
shutil.copyfile(src, dst, *, follow_symlinks=True)
:Copy the contents (no metadata) of the file named src to a file named dst and return dst. src and dst are path names given as strings. dst must be the complete target file name; look at shutil.copy() for a copy that accepts a target directory path. If src and dst specify the same file, SameFileError is raised.
https://docs.python.org/3/library/shutil.html#shutil.copyfile