假设您的计算机上有此文件夹结构:
/a你想要这些文件夹:
/a/b
/a/c
/b/b您需要创建四个目录,例如使用mkdir:
mkdir /a/b
mkdir /a/c
mkdir /b
mkdir /b/b编写一个程序,在给定现有和所需目录结构的情况下,该程序将输出需要创建的目录。
第一行有两个数字: n,现有目录的数目,以及m,所需目录的数目,下面n行有现有目录的列表,下面m行有所需的目录列表。
示例:
1 3
/a
/a/b
/a/c
/b/b需要创建的目录
示例:
/a/b
/a/c
/b
/b/b发布于 2011-02-16 03:27:34
在Python中,您只需使用os.makedirs()。请看这篇文章底部的文档
import sys
num_existing, num_required = map(int, next(sys.stdin).split())
sep = "/"
directory = {}
for i,path in enumerate(s.rstrip("\n") for s in sys.stdin):
d = directory
base = ""
for p in path.split(sep)[1:]:
base += sep+p
if p not in d:
if i >= num_existing:
print base
d[p]={}
d = d[p]用于os.makedirs()的Python
>>> import os
>>> help(os.makedirs)
Help on function makedirs in module os:
makedirs(name, mode=511)
makedirs(path [, mode=0777])
Super-mkdir; create a leaf directory and all intermediate ones.
Works like mkdir, except that any intermediate path segment (not
just the rightmost) will be created if it does not exist. This is
recursive.https://codegolf.stackexchange.com/questions/964
复制相似问题