#!/usr/bin/env python
# -*- coding: UTF-8 -*-
#######################################
#Describe:生成随机字符串命名的文件
#Date:2018-12-18
#Usage:2.py file_count
#file_count is the quantity of files
#######################################
import random
import string
import sys
import time
import os

STR=[chr(i) for i in range(65,91)]
str=[chr(i) for i in range(97,123)]
num=[chr(i) for i in range(48,58)]
special=[]
initspecial=string.punctuation		#string.punctuation可以获取全部特殊字符,输出为字符串形式
for i in initspecial:
	special.append(i)

total=STR+str+num+special			#total即为包含大小写字母,数字,特殊字符的列表

def mkfile(lenth):
		file_prefix_list=random.sample(total,int(lenth))
		file_prefix=''.join(file_prefix_list)
		now=time.strftime('%Y%m%d')
		filename='.'.join([file_prefix,now,'txt'])
		os.mknod(filename)

if __name__ == '__main__':
		num=sys.argv[1]
		if len(sys.argv[1:])<>1:
			exit('Exit:please input only one parm')
		if not num.isdigit():
			exit('Exit:The parm can only be Numbers')
		if  int(num)<1:
			exit('Exit:The parm has to be greater than 1')
		for i in range(int(num)):
			mkfile(5)

random用法,range用法,join用法

Logo

CSDN联合极客时间,共同打造面向开发者的精品内容学习社区,助力成长!

更多推荐