<small id='LCH9C'></small><noframes id='LCH9C'>

<tfoot id='LCH9C'></tfoot>

    <i id='LCH9C'><tr id='LCH9C'><dt id='LCH9C'><q id='LCH9C'><span id='LCH9C'><b id='LCH9C'><form id='LCH9C'><ins id='LCH9C'></ins><ul id='LCH9C'></ul><sub id='LCH9C'></sub></form><legend id='LCH9C'></legend><bdo id='LCH9C'><pre id='LCH9C'><center id='LCH9C'></center></pre></bdo></b><th id='LCH9C'></th></span></q></dt></tr></i><div id='LCH9C'><tfoot id='LCH9C'></tfoot><dl id='LCH9C'><fieldset id='LCH9C'></fieldset></dl></div>

    • <bdo id='LCH9C'></bdo><ul id='LCH9C'></ul>
    1. <legend id='LCH9C'><style id='LCH9C'><dir id='LCH9C'><q id='LCH9C'></q></dir></style></legend>

      如何将文件复制到 Python 脚本中的特定文件夹?

      How to copy a file to a specific folder in a Python script?(如何将文件复制到 Python 脚本中的特定文件夹?)
        <tbody id='SAufa'></tbody>

          • <bdo id='SAufa'></bdo><ul id='SAufa'></ul>

            <tfoot id='SAufa'></tfoot>
            <legend id='SAufa'><style id='SAufa'><dir id='SAufa'><q id='SAufa'></q></dir></style></legend>

            <small id='SAufa'></small><noframes id='SAufa'>

            • <i id='SAufa'><tr id='SAufa'><dt id='SAufa'><q id='SAufa'><span id='SAufa'><b id='SAufa'><form id='SAufa'><ins id='SAufa'></ins><ul id='SAufa'></ul><sub id='SAufa'></sub></form><legend id='SAufa'></legend><bdo id='SAufa'><pre id='SAufa'><center id='SAufa'></center></pre></bdo></b><th id='SAufa'></th></span></q></dt></tr></i><div id='SAufa'><tfoot id='SAufa'></tfoot><dl id='SAufa'><fieldset id='SAufa'></fieldset></dl></div>

                本文介绍了如何将文件复制到 Python 脚本中的特定文件夹?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着跟版网的小编来一起学习吧!

                问题描述

                我有一个文件的路径存储在一个变量(比如)filePath 中.我想将该特定文件复制到 Python 脚本中的另一个特定文件夹.

                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.

                我试过了

                folderPath = (os.getcwd() + "/folder_name/") #to get the path of the folder
                shutil.copyfile(filePath, folderPath)
                

                但是我得到一个错误IOError: [Errno 21] Is a directory.

                我该如何解决这个问题?

                How can I solve this ?

                我的问题似乎与 如何复制python中的文件?.但实际上,我想将文件复制到 文件夹/目录,而该问题的大多数答案都提到将一个文件复制到另一个 文件.

                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/directory whereas most answers to that question mention copying one file to another file.

                推荐答案

                使用 shutil.copy(filePath, folderPath) 而不是 shutil.copyfile().这将允许您指定一个文件夹作为目标并复制包含权限的文件.

                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(src, dst, *, follow_symlinks=True):

                将文件 src 复制到文件或目录 dst.src 和 dst 应该是字符串.如果 dst 指定一个目录,该文件将使用来自 src 的基本文件名复制到 dst.返回新创建文件的路径.

                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() 复制文件数据和文件的权限模式(参见 os.chmod()).其他元数据,如文件的创建和修改时间,不会被保留.要保留原始文件的所有元数据,请改用 copy2().

                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() 本身中也记录的复制差异:

                See the difference in copying also documented in shutil.copyfile() itself:

                shutil.copyfile(src, dst, *, follow_symlinks=True):

                复制名为 src 的文件的内容(无元数据)到名为 dst 的文件 并返回 dst.src 和 dst 是以字符串形式给出的路径名.dst 必须是完整的目标文件名;查看 shutil.copy() 以获取接受目标目录路径的副本.如果 src 和 dst 指定相同的文件,则会引发 SameFileError.

                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

                这篇关于如何将文件复制到 Python 脚本中的特定文件夹?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持跟版网!

                本站部分内容来源互联网,如果有图片或者内容侵犯了您的权益,请联系我们,我们会在确认后第一时间进行删除!

                相关文档推荐

                Initialize Multiple Numpy Arrays (Multiple Assignment) - Like MATLAB deal()(初始化多个 Numpy 数组(多重赋值) - 像 MATLAB deal())
                How to extend Python class init(如何扩展 Python 类初始化)
                What#39;s the difference between dict() and {}?(dict() 和 {} 有什么区别?)
                What is a wrapper_descriptor, and why is Foo.__init__() one in this case?(什么是 wrapper_descriptor,为什么 Foo.__init__() 在这种情况下是其中之一?)
                Initialize list with same bool value(使用相同的布尔值初始化列表)
                setattr with kwargs, pythonic or not?(setattr 与 kwargs,pythonic 与否?)
                  <bdo id='SYWLM'></bdo><ul id='SYWLM'></ul>
                  <tfoot id='SYWLM'></tfoot>

                          <tbody id='SYWLM'></tbody>

                      1. <i id='SYWLM'><tr id='SYWLM'><dt id='SYWLM'><q id='SYWLM'><span id='SYWLM'><b id='SYWLM'><form id='SYWLM'><ins id='SYWLM'></ins><ul id='SYWLM'></ul><sub id='SYWLM'></sub></form><legend id='SYWLM'></legend><bdo id='SYWLM'><pre id='SYWLM'><center id='SYWLM'></center></pre></bdo></b><th id='SYWLM'></th></span></q></dt></tr></i><div id='SYWLM'><tfoot id='SYWLM'></tfoot><dl id='SYWLM'><fieldset id='SYWLM'></fieldset></dl></div>

                      2. <legend id='SYWLM'><style id='SYWLM'><dir id='SYWLM'><q id='SYWLM'></q></dir></style></legend>

                        <small id='SYWLM'></small><noframes id='SYWLM'>