Created
December 5, 2020 16:03
-
-
Save kotoripiyopiyo/bd7065b4be80b8420ce7bec033ad83a7 to your computer and use it in GitHub Desktop.
yyyy-mm-ddが名前のファイルを大量生成
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import os , random , re , shutil | |
extention_list = ['.txt' , '.docx' , '.jpg'] | |
project_path = './chapter09/project01' | |
os.makedirs(project_path, exist_ok=True) | |
for i in range(100): | |
yyyy = random.randint(1972,2020) | |
mm = random.randint(1,12) | |
if mm < 10: | |
mm = str(0) + str(mm) | |
dd = random.randint(1,30) | |
if dd < 10: | |
dd = str(0) + str(dd) | |
random_filename = str(yyyy) + '-' + str(mm) + '-' + str(dd) + extention_list[random.randint(0,4)] | |
filename = open(os.path.join(project_path , random_filename) , 'w') | |
filename.close() |
改良
import os , random , re , shutil
extention_list = ['.txt' , '.docx' , '.jpg']
project_path = './chapter09/project01'
os.makedirs(project_path, exist_ok=True)
for i in range(200):
yyyy = random.randint(1972,2020)
mm = random.randint(1,12)
if mm < 10:
mm = str(0) + str(mm)
dd = random.randint(1,30)
if dd < 10:
dd = str(0) + str(dd)
random_filename = f'{str(mm)}-{str(dd)}-{str(yyyy)}-{extention_list[random.randint(0,2)]}'
filename = open(os.path.join(project_path , random_filename) , 'w')
filename.close()```
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
退屈なことはPythonにやらせよう9.3でファイルがたくさん必要になったとき用