这是我的文件系统的示例结构,我在.text文件中有文件夹名,我在Shell脚本或python中调用这个文件如何递归地创建这个文件结构,我只需要下面结构中的文件夹名?你知道吗
d-r-x - moka.babu HDFS 0 2018-08-23 12:58 /HCDLPRD/user
d-r-x - moka.babu HDFS 0 2018-08-23 12:58 /hcdlprd/user/moka.babu
d-r-x - moka.babu HDFS 0 2018-08-23 12:58 /hcdlprd/user/moka.babu/hive
使用Python
OP声明
ls -ltr
被显式复制到一个文件中。我们可以先用awk
清理它,只将文件放入文件中这将把每一行按多个空格分成几部分,并将文件名(在第9列中)发送到该文件,产生以下结果:
在python中:
在bash中
检查@hansolo的答案,因为这实际上是相同的
编辑:以防目录中的一个文件夹可能不存在
如果您有文件夹: /hcdlprd/user/head/some/dir/文件.txt你知道吗
如果
head
不是由脚本在这一行之前创建的,您可以创建一个更健壮的解决方案:如果
awk
的概念化有点奇怪,我们可以用python包装所有内容,方法是用以下代码处理每一行:您可以读取中的行,用
.split(' ')
分割空间中的每一行,然后索引最后一个值以获得文件夹名称。然后就是import os
后跟for folder in folders:
os.mkdir(folder)
下面是一个带有基本错误检查的示例,它将打印无法创建的文件:
在任何壳中:
该命令将创建整个结构。你知道吗
相关问题 更多 >
编程相关推荐