下面的脚本应该可以完成这项工作:
#!/usr/bin/env python3
import random
import os
import subprocess
import shutil
# set the desired number of folders to pick below
n_selection = 5
# set the name of the flash drive below
flashdr = "Lexar"
# set the source directory (with media folders) below
sourcedr = "/path/to/mediafiles"
# ---
try:
targetdr = [l.split("part ")[-1] for l in subprocess.check_output("lsblk")\
.decode("utf-8").splitlines()if l.endswith(flashdr)][0]
except IndexError:
pass
else:
# empty the flash drive
for item in os.listdir(targetdr):
obj = os.path.join(targetdr, item)
try:
shutil.rmtree(obj)
except NotADirectoryError:
os.remove(obj)
# list the source dirs
srclist = []
for dr in os.listdir(sourcedr):
fullpath = os.path.join(sourcedr, dr)
if os.path.isdir(fullpath):
srclist.append([dr, fullpath])
# copy the files
for picked in random.sample(srclist, n_selection):
shutil.copytree(picked[1], os.path.join(targetdr, picked[0]))
srclist.remove(picked)
Run Code Online (Sandbox Code Playgroud)
它将目录从源目录的第一个子级别复制到目标闪存驱动器中。
这似乎是最有意义的,因为随机递归复制文件夹会导致文件夹大小和数量子级别的巨大差异。尽管如此,我还是在这个答案的底部添加了它作为第二个选项。
create_mediausb.py
使用以下命令运行脚本:
python3 /path/to/create_mediausb.py
Run Code Online (Sandbox Code Playgroud)如果一切正常,请将其添加到快捷键:选择:系统设置>“键盘”>“快捷方式”>“自定义快捷方式”。单击“+”并添加命令:
python3 /path/to/create_mediausb.py
Run Code Online (Sandbox Code Playgroud)python3 /path/to/create_mediausb.py
Run Code Online (Sandbox Code Playgroud)
剧本:
查找闪存驱动器的路径:
python3 /path/to/create_mediausb.py
Run Code Online (Sandbox Code Playgroud)
如果未找到闪存驱动器,则到此结束
如果找到驱动器,则将其清空
#!/usr/bin/env python3
import random
import os
import subprocess
import shutil
n_selection = 5
flashdr = "Lexar"
sourcedr = "/home/jacob/Bureaublad/GW_site_nafestival_2015/pix/general"
try:
targetdr = [l.split("part ")[-1] for l in subprocess.check_output("lsblk")\
.decode("utf-8").splitlines()if l.endswith(flashdr)][0]
except IndexError:
pass
else:
# empty the flash drive
for item in os.listdir(targetdr):
obj = os.path.join(targetdr, item)
try:
shutil.rmtree(obj)
except NotADirectoryError:
os.remove(obj)
# list the source dirs
srclist = []
for root, dirs, files in os.walk(sourcedr):
for dr in dirs:
srclist.append([dr, os.path.join(root, dr)])
# copy the files
for picked in random.sample(srclist, n_selection):
shutil.copytree(picked[1], os.path.join(targetdr, picked[0]))
srclist.remove(picked)
Run Code Online (Sandbox Code Playgroud)然后,由于我们需要整个目录列表随机进行适当的选择,因此我们在进行选择之前创建一个列表:
try:
targetdr = [l.split("part ")[-1] for l in subprocess.check_output("lsblk")\
.decode("utf-8").splitlines()if l.endswith(flashdr)][0]
except IndexError:
pass
Run Code Online (Sandbox Code Playgroud)最有趣的部分是进行选择。我们随机选择一个目录,将其从列表中删除以防止重复选择,然后随机选择另一个目录,将其删除,依此类推,直到达到所需选择的数量:
# empty the flash drive
for item in os.listdir(targetdr):
obj = (targetdr+"/"+item)
try:
shutil.rmtree(obj)
except NotADirectoryError:
os.remove(obj)
Run Code Online (Sandbox Code Playgroud)小智 7
你可以使用 find 和 shuf:
#!/bin/bash
SOURCE="path/to/source"
DESTINATION="path/to/destination"
COUNT=25
rm -r "${DESTINATION}/"*
find "$SOURCE" -mindepth 2 -maxdepth 2 -type d|shuf -n $COUNT|xargs -d'\n' -I{} cp -r "{}" "$DESTINATION"
Run Code Online (Sandbox Code Playgroud)
小智 1
我用Python编写了一个脚本:
用法:
python3 RandomCopier.py [source folder] [destination folder] [number to copy]
Run Code Online (Sandbox Code Playgroud)
复制方法:
注意:它不会直接复制源文件夹中的任何文件,只会复制其子文件夹中的文件。
比如说,源文件夹src
是:
src
|- a
| |- file_a
| |- file_a_2
|
|- b
| |- file_b
|
|- c
| |- file_c
|
|- file_src
Run Code Online (Sandbox Code Playgroud)
然后,目标文件夹dest
将是随机复制的 2 个文件夹,例如:
dest
|- a
| |- file_a
| |- file_a_2
|
|- c
| |- file_c
Run Code Online (Sandbox Code Playgroud)
归档时间: |
|
查看次数: |
1442 次 |
最近记录: |