如何将文件复制到 Python 脚本中的特定文件夹?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/45126572/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to copy a file to a specific folder in a Python script?
提问by dravid07
I have the path of a file stored in a variable (say) filePath. I would like to copy that particular file to another specific folder within a Python script.
我将文件的路径存储在变量(比如)filePath 中。我想将该特定文件复制到 Python 脚本中的另一个特定文件夹。
I tried
我试过
folderPath = (os.getcwd() + "/folder_name/") #to get the path of the folder
shutil.copyfile(filePath, folderPath)
But I got an error IOError: [Errno 21] Is a directory
.
但我有一个错误 IOError: [Errno 21] Is a directory
。
How can I solve this ?
我该如何解决这个问题?
My question might seem to be a duplicate of How do I copy a file in python? . But actually, I want to copy a file to a folder/directorywhereas most answers to that question mention copying one file to another file.
我的问题似乎与如何在 python 中复制文件重复? . 但实际上,我想将文件复制到文件夹/目录,而该问题的大多数答案都提到将一个文件复制到另一个文件。
采纳答案by Hendrik Makait
Use shutil.copy(filePath, folderPath)
instead of shutil.copyfile()
. This will allow you to specify a folder as the destination and copies the file including permissions.
使用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.
shutil.copy(src, dst, *, follow_symlinks=True)
:将文件 src 复制到文件或目录 dst。src 和 dst 应该是字符串。如果 dst 指定了一个目录,则该文件将使用来自 src 的基本文件名复制到 dst。返回新创建文件的路径。
...
copy() 复制文件数据和文件的权限模式(参见 os.chmod())。其他元数据,如文件的创建和修改时间,不会保留。要保留原始文件中的所有文件元数据,请改用 copy2()。
https://docs.python.org/3/library/shutil.html#shutil.copy
https://docs.python.org/3/library/shutil.html#shutil.copy
See the difference in copying also documented in shutil.copyfile()
itself:
查看复制的差异也记录在shutil.copyfile()
其本身:
shutil.copyfile(src, dst, *, follow_symlinks=True)
:Copy the contents (no metadata)of the file named src to a file named dstand 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.
shutil.copyfile(src, dst, *, follow_symlinks=True)
:将名为 src的文件的内容(无元数据)复制到名为 dst 的文件并返回 dst。src 和 dst 是以字符串形式给出的路径名。dst 必须是完整的目标文件名;查看shutil.copy() 以获取接受目标目录路径的副本。如果 src 和 dst 指定相同的文件,则引发 SameFileError 。
https://docs.python.org/3/library/shutil.html#shutil.copyfile
https://docs.python.org/3/library/shutil.html#shutil.copyfile
回答by Sam Chats
folderpath
must be a file, not a directory. The error says it all. Do something like:
folderpath
必须是文件,而不是目录。错误说明了一切。做类似的事情:
shutil.copyfile(filePath, folderPath+'/file_copy.extension')
回答by williezh
Change your code as below:
更改您的代码如下:
folderPath = os.path.join('folder_name', os.path.basename(filePath))
shutil.copyfile(filePath, folderPath)