0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 3 years have passed since last update.

Pythonでリストを読み込んで複数フォルダ作成

Last updated at Posted at 2020-08-03

同一フォルダにあるfol.txtを読みこんで、
そこに記載されている名前のフォルダを作成する

fol.txt(例)
AAA
BBB
CCC

だと、AAA, BBB, CCCのフォルダが作成される

短いVer

import os
f = open('fol.txt')
areas = f.read().split()
f.close()
for area in areas:
    if os.path.exists(area):
        print('フォルダ ' + area + ' は既に存在しています')
    else:
        os.mkdir(area)

短いVer2 コメントを参考に修正しました

import os
with open('test.txt','r') as f:
  for row in f:
    print(row.strip())
    if os.path.exists(row.strip()):
        print('フォルダ ' + row.strip() + ' は既に存在しています')
    else:
        os.mkdir(row.strip())

長いVer


import glob
import re
import os

f = open(r'fol.txt', 'r')
for line in f:
    
    myPath=os.getcwd()
    print(myPath)
    text=r"{}\{}"
    result=text.format(myPath,line.rstrip('\n'))
    print(result)
    os.mkdir(result)
    
f.close 

参考サイト
https://qiita.com/zaburo/items/0ba12417dfb39fcb1555

0
2
2

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
2

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?