Skip to content

Instantly share code, notes, and snippets.

@kotoripiyopiyo
Created December 5, 2020 16:03
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save kotoripiyopiyo/bd7065b4be80b8420ce7bec033ad83a7 to your computer and use it in GitHub Desktop.
Save kotoripiyopiyo/bd7065b4be80b8420ce7bec033ad83a7 to your computer and use it in GitHub Desktop.
yyyy-mm-ddが名前のファイルを大量生成
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()
@kotoripiyopiyo
Copy link
Author

退屈なことはPythonにやらせよう9.3でファイルがたくさん必要になったとき用

@kotoripiyopiyo
Copy link
Author

kotoripiyopiyo commented Dec 6, 2020

改良

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