【问题标题】:Use python with mobaxterm to copy 25 random files from one folder to another使用python和mobaxterm将25个随机文件从一个文件夹复制到另一个文件夹
【发布时间】:2020-04-16 11:59:55
【问题描述】:

我正在尝试编写一个 python 脚本,我可以使用 mobaxterm 从一个文件夹中抓取 25 个随机文件并将它们复制到另一个文件夹中。

我对使用 mobaxterm 非常陌生,我的实习主要是编写小脚本来帮助我的同事自动化日常工作,因此我们非常感谢任何建议!我可以打开 mobaxterm 并导航到保存此 python 脚本的文件夹,但出现以下错误:

IOError: [Errno 21] Is a directory: 

我现在的代码如下

import shutil, random, os

dirpath = '/SSH_folder_where_stuff_is/images/' 

destDirectory = '/SSH_folder_where_stuff_should_go/'

filenames = random.sample(os.listdir(dirpath), 25)
for fname in filenames:
    srcpath = os.path.join(dirpath, fname)
    shutil.copyfile(srcpath, destDirectory)
    print('done!')

提前感谢您的任何建议!

【问题讨论】:

  • 如何运行脚本?看起来它正在尝试通过bash 而不是python 运行它。您可能需要在脚本顶部添加 shebang
  • @0x5453 你好!我通过调用 python 然后是文件路径来运行它,你对它最初运行 bash 的看法是正确的。

标签: python linux ssh mobaxterm


【解决方案1】:

您需要更改您的 shutil 用法。 shutil 有两个文件名,但在您的代码中,第二个是目录。

这是一个更正的版本。

import shutil, random, os

dirpath = '/SSH_folder_where_stuff_is/images/' 
destDirectory = '/SSH_folder_where_stuff_should_go/'

filenames = random.sample(os.listdir(dirpath), 5)
for fname in filenames:
    srcpath = os.path.join(dirpath, fname)
    # Change the second parameter here.
    shutil.copyfile(srcpath, destDirectory+fname)
    print('done!')

【讨论】:

  • 我觉得很傻,没有意识到这是问题所在,非常感谢!
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2014-12-02
  • 1970-01-01
  • 2023-01-14
  • 2011-08-22
相关资源
最近更新 更多