Chapter 6. Files and Directories
创建目录结构的代码
# create_tree.rb
def create_tree(directories, parent=".")
directories.each_pair do |dir, files|
path = File.join(parent, dir)
Dir.mkdir path unless File.exists? path
files.each do |filename, contents|
if filename.respond_to? :each_pair # It's a subdirectory
create_tree filename, path
else # It's a file
open(File.join(path, filename), 'w') { |f| f << contents || "" }
end
end
end
end
Now I can present th directory structure as a data structure and you can create it with a single method call:
require 'create_tree'
create_tree 'test' =>
[ 'An empty file',
['A file with contents', 'Contents of file'],
{ 'Subdirectory' => ['Empty file in subdirectory',
['File in subdirectory', 'Contents of file'] ] },
{ 'Empty subdirectory' => [] }
]
require 'find'
Find.find('test') { |f| puts f }
# test
# test/Empty subdirectory
# test/Subdirectory
# test/Subdirectory/File in subdirectory
# test/Subdirectory/Empty file in subdirectory
# test/A file with contents
# test/An empty file
File.read('test/Subdirectory/File in subdirectory')
# => "Contents of file"
==============
有点疑问,string也可以each |key,value|么?试下
irb(main):019:0> 'a'.each do | t,td|
irb(main):020:1* puts 1 if t
irb(main):021:1> puts 2 if td
irb(main):022:1> end
1
=> "a"
原来可以。。
分享到:
相关推荐
"cookbook-zh-CN.md"文件很可能是这本书的主要文本部分,里面详细列举了各种使用PySimpleGUI的技巧和实践案例。每个章节通常会介绍一个特定的功能或概念,并配有相应的代码示例。通过阅读和实践这些例子,开发者可以...
Odoo 14 Development Cookbook - Fourth Edition(ebook-eglish) 代码
python-machine-learning-cookbook-preprocessing oreilly 英文 epub格式
标题“coverage-cookbook-complete-verification-academy”表明这是一本关于覆盖度(coverage)的食谱手册,隶属于Cadence Academy的官方文件。这种手册通常包含一系列经过精心设计的指导方案,旨在帮助读者理解和...
Programming ArcGIS with Python Cookbook - Second Edition, mobi格式
Programming ArcGIS with Python Cookbook - Second Edition,epub格式
《CMake Cookbook》是关于构建、测试和打包模块化软件的专业指南,专注于现代CMake工具的使用。本书由Radovan Bast和Roberto Di Remigio合著,旨在帮助读者掌握CMake这一强大的跨平台构建系统。 CMake是一个开源的...
The 2019 edition of this popular cookbook provides more than 80 practical recipes to help you set up and use this open source server to solve problems in various use cases. For professionals who ...
Linux Shell Scripting Cookbook - Third Edition by Clif Flynt English | 29 May 2017 | ASIN: B01N80F75Z | 552 Pages | AZW3 | 1.36 MB Do amazing things with the shell About This Book Become an expert ...
Bioinformatics-with-Python-Cookbook-Learn-how-to-use-modern-Python-bioinformatics-libraries-and-applications-to-do-cutting-edge-research-in-computational-biology.pdf
IPython-Interactive-Computing-and-Visualization-Cookbook-Over-100-hands-on-recipes-to-sharpen-your-skills-in-high-performance-numerical-computing-and-data-science-with-Python.pdf
docker run -tid -p <port>:80 apachecn0/pandas-cookbook-code-notes # 访问 http://localhost:{port} 查看文档 PYPI pip install pandas-cookbook-code-notes pandas-cookbook-code-notes # 访问 ...
Unity Game Development Cookbook - Paris Buttfield-AddisonUnity Game Development Cookbook - Paris Buttfield-Addison
Practical-Data-Science-Cookbook-89-hands-on-recipes-to-help-you-complete-real-world-data-science-projects-in-R-and-Python.pdf
Lott -- Modern Python Cookbook -- 2016 -- code.7z
Aggarwal -- Flask Framework Cookbook -- 2014 -- code.7z