在python中复制选定的文件

时间:2016-07-14 17:57:37

标签: python python-2.7

我试图编写一个Python脚本来复制扩展名为' .txt'的文件。从一个文件夹到另一个文件夹。我在命令行上键入以下内容:

python C:/Users/Username/Desktop/folder_name/copyselect.py C:/Users/Username/Desktop/folder_name C:Users/Username/Desktop/new_folder

这是代码:

import os
import sys
import shutil

def main():
  src = sys.argv[1]
  dst = sys.argv[2]

  for filenames, dirpath, dirnames in os.walk(src):
    if not os.path.isdir(dst):
      os.makedirs(dst)
    for filename in filenames:
      if filename.endswith('.txt'):
        shutil.copy2(os.path.join(src, filename), dst)

if __name__ == "__main__":
  main()

我一直收到以下错误消息:

IO Error: [Errno 2] No such file or directory: 'C:/Users/Username/Desktop/folder_name/C'

为什么这个神秘的' C'出现在' folder_name'?它似乎来自于' C'在命令行的最后一个文件路径中。这是Windows的事吗?我该如何修复此代码?

4 个答案:

答案 0 :(得分:1)

你得到了os.walk反转结果的顺序。应该是:

  for dirpath, dirnames, filenames in os.walk(src):
    if not os.path.isdir(dst):
      os.makedirs(dst)
    for filename in filenames:
      if filename.endswith('.txt'):
        shutil.copy2(os.path.join(src, filename), dst)

你错过了第二个参数的斜杠。 应该是:

python "C:/Users/Username/Desktop/folder_name/copyselect.py" "C:/Users/Username/Desktop/folder_name" "C:/Users/Username/Desktop/new_folder"

答案 1 :(得分:0)

将命令参数放在单引号...

之间
python C:/Users/Username/Desktop/folder_name/copyselect.py 'C:/Users/Username/Desktop/folder_name' 'C:Users/Username/Desktop/new_folder'

答案 2 :(得分:0)

#Copy the files with Two examples.
#If your are not expecting this answer, sorry.

#...................................................................
import os, shutil
src = os.path.abspath (sys.argv[1])
dst = os.path.abspath (sys.argv[2])

for filenames, dirpath, dirnames in os.walk(src):

    dstRoot     = dst + filenames.replace (src, '')

    if not os.path.isdir (dstRoot):
        os.makedirs (dstRoot)

    for dirname in dirnames:
        if dirname.endswith('.txt'):
            sourceDocu  = (filenames + '\\' + dirname)
            targetDocu  = (dstRoot + '\\' + dirname)            
            shutil.copy2 (sourceDocu, targetDocu)

#...................................................................
#Files copy from Single folder
import os, shutil
src = os.path.abspath (sys.argv[1])
dst = os.path.abspath (sys.argv[2])

fileList    = os.listdir (src)

if fileList :
    if not os.path.isdir (dst):
        os.makedirs (dst)

    for eachFile in fileList :        
         if eachFile.endswith('.txt') :             
            shutil.copy2 (src + '\\' + eachFile, dst + '\\' + eachFile)            

 #...................................................................            

答案 3 :(得分:0)

非常感谢您的回答!

进行这些编辑后,如果我为 src dst 添加文件路径,脚本就会运行,但我无法从命令行运行它

由于我喜欢搜索子文件夹的脚本,所以我添加了一行来处理它。该脚本现在看起来像这样:

import os
import sys
import shutil

def main():
  src = sys.argv[1]
  dst = sys.argv[2]

  for dirpath, dirnames, filenames in os.walk(src):
    if not os.path.isdir(dst):
      os.makedirs(dst)
    filenames = [os.path.join(dirpath, filename) for dirpath, dirnames, filenames in os.walk(src) for filename in filenames]
    for filename in filenames:
      if filename.endswith('.txt'):
        shutil.copy2(os.path.join(src, filename), dst)

if __name__ == '__main__':
  main()

在命令行中输入:

python C:/Users/Username/Desktop/folder_name/copyselect.py 'C:/Users/Username/Desktop/folder_name' 'C:/Users/Username/Desktop/new_folder'


有谁知道阻止此脚本在命令行运行的错误是什么?感谢。