本文的代码可以把指定文件夹中的所有文件名批量随机化。
from string import ascii_letters
from os import listdir, rename
from os.path import splitext, join
from random import choice, randint
def randomFilename(directory):
for fn in listdir(directory):
#切分,得到文件名和扩展名
name, ext = splitext(fn)
n = randint(5, 20)
#生成随机字符串作为新文件名
newName = ''.join((choice(ascii_letters) for i in range(n)))
#修改文件名
rename(join(directory, fn), join(directory, newName+ext))
randomFilename('C:\\test')
标签:join,文件名,Python,随机化,directory,import,fn
From: https://blog.51cto.com/u_9653244/6450810