尽可能方便地合并文件
假设我有以下降价文件
1.md # contains 'foo'
2.md # contains 'bar'
3.md # 'zoo'
4.md # 'zxc'
使用命令cat
$ cat {1..4}.md > merged_5.md
然而,Python需要多个步骤来实现这一结果。
创建读写方法
def read(filename):
with open(filename) as file:
content = file.read()
return content
def write(filename, content):
with open(filename, 'w') as file:
file.write(content)
检索限定文件
import glob
filenames = glob.glob('*.md')
In [17]: filenames
Out[17]: ['1.md', '2.md', '3.md', '4.md']
阅读并合并
def combine(filenames):
merged_conent = ""
for filename in filenames:
content = read(filename)
merged_content += content
write('merged.md', merged_content)
在main
模块中封装数据和方法,另存为' combine_files.py'
def main():
filenames = glob.glob('*.md')
combine(filenames)
if __name__ == '__main__':
main()
在命令行上运行
python3 combine_files.py
它并不像命令“猫”那样方便。
如何重构代码以尽可能方便?
答案 0 :(得分:1)
怎么样?:
def fun1():
mysum = 0
def fun2(arg):
mysum += arg
fun2(1)
fun2(2)
fun2(3)
print mysum
答案 1 :(得分:0)
如何轻松做到:
def cat(out, *src):
'''Concatenate files'''
with open(out, 'wb') as f:
data = b'\n'.join(open(i, 'rb').read() for i in src)
f.write(data)
您现在可以使用cat('merged.md', glob.glob('*.md'))
来呼叫它。怎么样方便?当然比GNU Coreutils的来源容易得多。