在 python 中创建/制作目录(复杂)
我正在尝试创建一堆可以将文件复制到其中的目录/子目录。我正在使用Python,但我似乎找不到一个好的方法来做到这一点。我有一条主路,我将从中分支出来。之后,我有 Weights 和 No_Weights。男性和女性跟随。在每个男性和女性文件夹中,我都有每个种族(白种人、非裔美国人、亚洲人、西班牙裔、印度人、其他人、未知)。在每个文件夹中,我的年龄范围从 20 岁以下一直到 70 岁以上(B20、20、30、40、50、60、70)。
我尝试生成所有路径,因此我只需调用 mkdir 大约 50 次,但这大约有 150 行代码(几乎)。
有没有一种简单的方法可以创建所有这些文件夹而无需手动完成?
I am trying to create a bunch of directories/subdirectories that I can copy files into. I am working with Python and I can't seem to find a good way to do this. I have a main path that I will branch off of. Then after that, I have Weights and No_Weights. Male and Female following. Within each of Male and Female folders, I have each ethnicity (Caucasian, African-American, Asian, Hispanic, Indo, Other, Unknown). Within each of those folders, I have age ranges from Below 20, all the way to 70+ (B20, 20, 30, 40, 50, 60, 70).
I have tried to generate all of the paths so all I would have to call is mkdir about 50 times, but that is about 150 lines of code (almost).
Is there any simple way to create all of these folders without having to do it by hand?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
itertools.product() 将构建所有可能的路径变体,然后 os.path.join() 将使用适合您平台的正确语法将子路径连接在一起。
编辑:需要
os.makedirs()
而不是os.mkdir()
。只有前者才会构建完整路径中的所有中间子目录。itertools.product()
will construct all possible path variations, thenos.path.join()
will join the subpaths together using the correct syntax for your platform.EDIT:
os.makedirs()
is needed instead ofos.mkdir()
. Only the former will construct all the intermediate subdirectories in a full path.这个例子应该可以帮助您入门:
itertools 模块是您的朋友:
http://docs.python.org/library/itertools.html#itertools。产品
This example should get you started:
The itertools module is your friend:
http://docs.python.org/library/itertools.html#itertools.product
只需做这样的事情:
这应该会为你解决......
Just do something like this:
and that should take care of it for you...
有几个嵌套的 for 循环,然后每个循环都有 os.mkdir 。使用 os.path.join 将目录路径连接在一起。
像这样的东西:
这里循环只是一个普通的 for 循环:
mkdir is os.mkdir
'+' is os.path.join:
编辑: dir_util 可能在这里有用,所以你不必创建每个子目录:
http://docs.python.org/release/2.5。 2/dist/module-distutils.dirutil.html
Have a few nested for-loops, then os.mkdir for each one. Use os.path.join to concatenate the directory path together.
Something like:
here loop is just a normal for-loop:
mkdir is os.mkdir
'+' is os.path.join:
Edit: dir_util might be of some use here so you don't have to make each subdirectory:
http://docs.python.org/release/2.5.2/dist/module-distutils.dirutil.html
os.makedirs 可以提供帮助 - 它使所有中间目录一直到您指定的“叶”目录。
另一个问题(生成所有“A 列中的一个,B 列中的一个,...”组合)最好作为“混合基数计数”问题来解决 - 粗略地说,s/thing like...:
< code>itertools.product 是一种现代而简洁的方法,用于生成所有“A 列中的一个,等等”选择,以及我在生产软件中的建议 - 只是:
可以替换所有上述代码( !)。我认为还值得了解“混合基计数”较低抽象级别的方法,因为它在许多情况下都很方便(包括使用 Python 版本
<2.6
;-)。os.makedirs can help -- it makes all intermediate directories all the way down to the "leaf" one you specify.
The other issue (generating all the "one from column A, one from column B, ..." combinations) is best approached as a problem of "counting in mixed bases" -- roughly, s/thing like...:
itertools.product
is the modern and concise approach to generating all the "one from column A, etc etc" picks, and what I would advise in production software -- just:can replace all of the above code (!). I think it's also worth being aware of the "counting in mixed bases" lower-abstraction-level approach because it comes in handy in many cases (including times in which one is stuck using a Python release
< 2.6
;-).