-4

我想知道如何使用批处理文件或 Python 从目录中的多个文件夹中打开一个随机文件夹?

4

2 回答 2

2
>>> import random
>>> import os
>>> files = os.listdir('/tmp')
>>> dirs = [f for f in files if os.path.isdir(f)]
>>> random.sample(dirs,1)
['tempdir']
于 2013-03-27T20:45:49.373 回答
0

cmd你可以这样做:

@echo off

setlocal EnableDelayedExpansion

set root=C:\base\folder

for /f %%d in ('dir /b /a:d "%root%" ^| find /c /v ""') do set count=%%d

set /a num=%RANDOM% %% %count%

for /f "skip=%num% tokens=*" %%d in ('dir /b /a:d "%root%"') do (
  set folder=%%~fd
  goto :FIN
)

:FIN
echo %folder%

endlocal
于 2013-03-27T22:14:20.637 回答