Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/18.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 3.x Python3:在目录树中多次选择和复制随机文件_Python 3.x - Fatal编程技术网

Python 3.x Python3:在目录树中多次选择和复制随机文件

Python 3.x Python3:在目录树中多次选择和复制随机文件,python-3.x,Python 3.x,我对从目录树复制随机文件感兴趣。我尝试了下面的示例,它对一个目录的文件非常有效,但是我希望它搜索多个子目录并做同样的事情 例如: 我试图找出如何使用os.walk或shutil.copytree,但不知道该怎么做。提前谢谢 import os import shutil import random import os.path src_dir = 'C:\\' target_dir = 'C:\\TEST' src_files = (os.listdir(src_dir)) def vali

我对从目录树复制随机文件感兴趣。我尝试了下面的示例,它对一个目录的文件非常有效,但是我希望它搜索多个子目录并做同样的事情

例如:

我试图找出如何使用os.walk或shutil.copytree,但不知道该怎么做。提前谢谢

import os
import shutil
import random
import os.path

src_dir = 'C:\\'
target_dir = 'C:\\TEST'
src_files = (os.listdir(src_dir))
def valid_path(dir_path, filename):
    full_path = os.path.join(dir_path, filename)
    return os.path.isfile(full_path)  
files = [os.path.join(src_dir, f) for f in src_files if valid_path(src_dir, f)]
choices = random.sample(files, 5)
for files in choices:
    shutil.copy(files, target_dir)
print ('Finished!')
我已经更新了它,但是我注意到os.walk只给了我树中的最后一个目录,它有5个文件,而不是根目录中的15个文件。不确定我在这里哪里出了问题:

import os
import shutil
import random

source_dir = "C:\\test\\from"
target_dir = "C:\\test\\to"  

for path, dirs, filenames in os.walk(source_dir):
    source_files = filenames
    print(source_files) # this gives me 15 files whih is correct

print(source_files) # this gives me only 5 files which is only the last directory

choices = random.sample(source_files, 5)
print(choices)


for files in choices:
    shutil.copy(files, target_dir)

您可以尝试使用递归方法来实现这一点: 在这里你可以找到答案

https://stackoverflow.com/questions/2212643/python-recursive-folder-read/55193831#55193831

好的,非常感谢您提供的信息,我现在有一个工作程序,可以从我的电脑中随机收集MP3,并将它们放在我的手机上。对于感兴趣的人,代码如下:

import os
import shutil
import random

# source directory for files to copy from
source_dir = "E:\\_Temp\\Music"
# target directory for files to copy to
target_dir = "C:\\test\\to"  

 # empty list for collecting files
source_files = []  

# walk through directory tree and find files only
for dirpath, dirnames, filenames in os.walk(source_dir):
    for file in filenames:
        if file.endswith(".mp3"):
            source_files.append(os.path.join(dirpath, file))

# select 300 files randomly               
choices = random.sample(source_files, 300)
print(choices)

# copy files to target directory
for files in choices:
    shutil.copy(files, target_dir)

谢谢,我通读了这篇文章,我有点卡住了,我觉得我很接近,因为我有随机计数部分工作,复制部分工作,但我无法让os.walk将所有文件存储为变量-只有最后一个文件夹。例如:
import os import shutil import random source\u dir=“C:\\test\\from”target\u dir=“C:\\test\\to”for path,dirs,os.walk(source\u dir)中的文件名:source\u files=filenames print(source\u files)
为我提供了正确的文件,总共15个。当我执行
import os import shutil import shutil import random source\u dir=“C:\\test\\from”target_dir=“C:\\test\\to”对于os.walk(source_dir)中的路径、目录、文件名:source_files=filenames print(source_files
我只得到5个文件(最后一行现在不是有意的)。您可以尝试从应答中选择:
os.path.isdir(filename)
,如果是目录,则可以将其存储到列表中。Hopw ir可能会有所帮助