Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/308.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/file/3.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/clojure/3.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脚本中的特定文件夹?_Python_File_Copy_Filesystems_File Copying - Fatal编程技术网

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

如何将文件复制到Python脚本中的特定文件夹?,python,file,copy,filesystems,file-copying,Python,File,Copy,Filesystems,File Copying,我将文件的路径存储在变量(比如)filePath中。我想将该特定文件复制到Python脚本中的另一个特定文件夹中 我试过了 folderPath = (os.getcwd() + "/folder_name/") #to get the path of the folder shutil.copyfile(filePath, folderPath) 但是我得到了一个错误,IOError:[Errno 21]是一个目录 我怎样才能解决这个问题 我的问题似乎是重复的。但实际上,我想把一个文件复制到

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

我试过了

folderPath = (os.getcwd() + "/folder_name/") #to get the path of the folder
shutil.copyfile(filePath, folderPath)
但是我得到了一个错误,
IOError:[Errno 21]是一个目录

我怎样才能解决这个问题


我的问题似乎是重复的。但实际上,我想把一个文件复制到一个文件夹/目录,而这个问题的大多数答案都提到将一个文件复制到另一个文件

文件夹路径
必须是一个文件,而不是目录。错误说明了一切。做一些类似于:

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)

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

copy()复制文件数据和文件的权限模式(请参阅os.chmod()。其他元数据(如文件的创建和修改时间)不保留。要保留原始文件中的所有文件元数据,请改用copy2()

参见
shutil.copyfile()
本身中记录的复制差异:

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

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


是的,这同样有效:)