AWE*_*AWE 5 python bash perl awk sed
懒惰的我正考虑在一些文本文件中添加一列.
文本文件位于目录中,我想将目录名添加到文本文件中.
就像文件text.txt
夹中的文本文件一样the_peasant
:
has a wart
was dressed up like a witch
has a false nose
Run Code Online (Sandbox Code Playgroud)
会成为:
the_peasant has a wart
the_peasant was dressed up like a witch
the_peasant has a false nose
Run Code Online (Sandbox Code Playgroud)
然后我在其他文件夹中有类似的文本文件,名为"the_king"等.
我认为这是find命令,bash脚本和sed的组合,但我无法看透.有任何想法吗?
目录树:
\n\n% tree .\n.\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 the_king\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 text.txt\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 the_knight\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 text.txt\n\xe2\x94\x9c\xe2\x94\x80\xe2\x94\x80 the_peasant\n\xe2\x94\x82\xc2\xa0\xc2\xa0 \xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 text.txt\n\xe2\x94\x94\xe2\x94\x80\xe2\x94\x80 wart.py\n3 directories, 4 files\n
Run Code Online (Sandbox Code Playgroud)\n\n之前的目录和内容:
\n\n% find . -name \'text.txt\' -print -exec cat {} \\; \n./the_king/text.txt\nhas a wart \nwas dressed up like a witch \nhas a false nose\n./the_knight/text.txt\nhas a wart \nwas dressed up like a witch \nhas a false nose\n./the_peasant/text.txt\nhas a wart \nwas dressed up like a witch \nhas a false nose\n
Run Code Online (Sandbox Code Playgroud)\n\n代码(wart.py):
\n\n#!/usr/bin/env python\n\nimport os\n\ntext_file = \'text.txt\'\ncwd = os.path.curdir # \'.\'\n\n# Walk thru each directory starting at \'.\' and if the directory contains\n# \'text.txt\', print each line of the file prefixed by the name containing\n# directory.\nfor root, dirs, files in os.walk(cwd):\n if text_file in files: # We only care IF the file is in this directory.\n print \'Found %s!\' % root\n filepath = os.path.join(root, text_file) # \'./the_peasant/text.txt\'\n root_base = os.path.basename(root) # \'./the_peasant\' => \'the_peasant\'\n output = \'\'\n with open(filepath, \'r\') as reader: # Open file for read/write\n for line in reader: # Iterate the lines of the file\n new_line = "%s %s" % (root_base, line)\n print new_line,\n output += new_line # Append to the output\n\n with open(filepath, \'w\') as writer:\n writer.write(output) # Write to the file\n\n print\n
Run Code Online (Sandbox Code Playgroud)\n\n哪个输出:
\n\nFound ./the_king!\nthe_king has a wart \nthe_king was dressed up like a witch \nthe_king has a false nose\n\nFound ./the_knight!\nthe_knight has a wart \nthe_knight was dressed up like a witch \nthe_knight has a false nose\n\nFound ./the_peasant!\nthe_peasant has a wart \nthe_peasant was dressed up like a witch \nthe_peasant has a false nose\n
Run Code Online (Sandbox Code Playgroud)\n\n之后的目录和内容:
\n\n% find . -name \'text.txt\' -print -exec cat {} \\;\n./the_king/text.txt\nthe_king has a wart \nthe_king was dressed up like a witch \nthe_king has a false nose\n./the_knight/text.txt\nthe_knight has a wart \nthe_knight was dressed up like a witch \nthe_knight has a false nose\n./the_peasant/text.txt\nthe_peasant has a wart \nthe_peasant was dressed up like a witch \nthe_peasant has a false nose\n
Run Code Online (Sandbox Code Playgroud)\n\n这很有趣!感谢您的挑战!
\n 归档时间: |
|
查看次数: |
349 次 |
最近记录: |