gpt4 book ai didi

python - 随机选择一个目录下的100个文档

转载 作者:行者123 更新时间:2023-12-02 11:32:28 25 4
gpt4 key购买 nike

该目录下约有2000个文档。我想随机选择一些文档并自动将它们复制到新目录。

Some relevant information关于在某个目录下生成一个文档名称。

最佳答案

尝试:

import shutil, random, os
dirpath = 'your/read/location'
destDirectory = 'your/destination'

filenames = random.sample(os.listdir(dirpath), 100)
for fname in filenames:
srcpath = os.path.join(dirpath, fname)
shutil.copyfile(srcpath, destDirectory)

关于python - 随机选择一个目录下的100个文档,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11372054/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com