Ruby file操作cheatsheet

每次都要查,真是蛋疼,不如一次性总结一下,以后再不记得就来这里找好了。
以下代码中需要用到的文件名:filename = ‘testfile.txt’

  • 读取其中的全部内容:File.read(filename)

  • 将一个字符串一次性写入这个文件:
    File.write(filename, str)

  • 读取文件内容,依次处理其中的每一行:

File.foreach(filename) do |line|#process each line
end
  • 判断文件是否存在:File.exists?(filename)

  • 删除文件:File.delete(filename)

  • 文件重命名:File.rename(filename, new_name)

  • 文件的名字:file.path # file是一个File对象

  • 文件的绝对路径:File.absolute_path(filename)

  • 当前所在目录:Dir.pwd

  • 当前登录用户的home目录:Dir.home

  • 创建文件夹:Dir.mkdir(dir_name) #注意,如果父目录不存在的话,这里无法创建子目录

  • 删除文件夹:Dir.rmdir(dir_name)

  • 创建多级目录(mkdir -p):FileUtils.mkdir_p(path) #这里需要require fileutils

  • 找出当前目录下的所有文件或目录:Dir.glob("*")

  • 找出当前目录下的所有Ruby文件:Dir.glob("*.rb")

  • 判断目录是否存在:Dir.exists?(dir_name)

  • 用文件夹和文件组成一个路径:File.join(dir_name, filename)

  • 当前文件(正在执行的文件)相对于当前所在目录(pwd目录)的相对路径:__FILE__

  • 当前文件(正在执行的文件)的目录相对于当前所在目录(pwd目录)的相对路径:File.dirname(__FILE__)

  • 当前文件(正在执行的文件)的绝对路径:File.expand_path(__FILE__)

  • 当前文件(正在执行的文件)所在目录的绝对路径:File.expand_path(File.dirname(__FILE__))

最后附上File.open(filename, mode) 中的mode各种取值以及含义:

ModeMeaning
"r"Read-only, starts at beginning of file (default mode).
"r+"Read-write, starts at beginning of file.
"w"Write-only, truncates existing file, to zero length or creates a new file for writing.
"w+"Read-write, truncates existing file to zero lengthor creates a new file for reading and writing.
"a"Write-only, starts at end of file if file exists, otherwise creates a new file for writing.
"a+"Read-write, starts at end of file if file exists, otherwise creates a new file for reading and writing.
"b"Binary file mode (may appear with any of the key letters listed above). Suppresses EOL <-> CRLF conversion on Windows. And sets external encoding to ASCII-8BIT unless explicitly specified.
"t"Text file mode (may appear with any of the key letters listed above except "b").

有任何意见或建议,或者发现文中任何问题,欢迎留言!

更多文章请访问个人博客
作者:邹小创
Github:https://github.com/ChrisZou
邮件:happystriving@126.com

本文来自互联网用户投稿,该文观点仅代表作者本人,不代表本站立场。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如若转载,请注明出处:http://www.mzph.cn/news/544118.shtml

如若内容造成侵权/违法违规/事实不符,请联系多彩编程网进行投诉反馈email:809451989@qq.com,一经查实,立即删除!

相关文章

大厂也在用的 6种 数据脱敏方案,别做泄密内鬼

最近连着几天晚上在家总是接到一些奇奇怪怪的电话&#xff0c;“哥&#xff0c;你是 xxx 吧&#xff0c;我们这里是 xxx 高端男士私人会所...”&#xff0c;握草&#xff0c;我先是一愣&#xff0c;然后狠狠的骂了回去。一脸傲娇的转过头&#xff0c;面带微笑稍显谄媚&#xff…

在Python中使用OpenCV裁剪图像

What is Cropping? 什么是播种&#xff1f; Cropping is the removal of unwanted outer areas from a photographic or illustrated image. The process usually consists of the removal of some of the peripheral areas of an image to remove extraneous trash from the…

面渣逆袭:RocketMQ二十三问

1.为什么要使用消息队列呢&#xff1f;消息队列主要有三大用途&#xff0c;我们拿一个电商系统的下单举例&#xff1a;解耦&#xff1a;引入消息队列之前&#xff0c;下单完成之后&#xff0c;需要订单服务去调用库存服务减库存&#xff0c;调用营销服务加营销数据……引入消息…

vue项目打包体积大优化之-productionSourceMap设置

一、productionSourceMap 的作用 productionSourceMap 在构建时生成完整的 SourceMap 文件&#xff0c;默认情况下开启。生产环境中启用 productionSourceMap 有助于开发者调试代码&#xff0c;可以在浏览器的调试工具中查看到源文件中错误的代码位置&#xff0c;而不是编译后…

Java日志性能那些事(转)

在任何系统中&#xff0c;日志都是非常重要的组成部分&#xff0c;它是反映系统运行情况的重要依据&#xff0c;也是排查问题时的必要线索。绝大多数人都认可日志的重要性&#xff0c;但是又有多少人仔细想过该怎么打日志&#xff0c;日志对性能的影响究竟有多大呢&#xff1f;…

如何在Java中使ArrayList只读?

使ArrayList只读 (Making ArrayList Read-Only) Given an ArrayList, and we have to make it Read-Only in Java. 给定一个ArrayList&#xff0c;我们必须使其成为Java只读。 Read-Only: If we make ArrayList as Read-Only i.e. we can only read ArrayList and we cannot p…

33岁程序员的年中总结

作者 | 磊哥来源 | Java中文社群&#xff08;ID&#xff1a;javacn666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;人生在不同的阶段会有不同的生活方式和思考问题的角度&#xff0c;这是一件非常有趣的事~ 比如&#xff0c;我在 22 岁会想&…

减治求有重复元素的全排列

求n个元素的全排列的所有解可以用减治法&#xff1a;每次拎出一个数做前缀&#xff0c;对剩下的元素再求全排列&#xff0c;直至只剩一个元素。代码源自《算法分析与设计&#xff08;王晓东&#xff09;》&#xff0c;复杂度O(n&#xff01;) 1 //输出k~m的所有全排列2 void pe…

数据科学中的简单线性回归

简单线性回归 (Simple Linear Regression) A simple regression model could be a linear approximation of a causative relationship between two or additional variables. Regressions models are extremely valuable, as theyre one in every of the foremost common ways…

鹅厂一面,有关 ThreadLocal 的一切

1. 底层结构ThreadLocal 底层有一个默认容量为 16 的数组组成&#xff0c;k 是 ThreadLocal 对象的引用&#xff0c;v 是要放到 TheadLocal 的值public void set(T value) {Thread t Thread.currentThread();ThreadLocalMap map getMap(t);if (map ! null)map.set(this, valu…

再战“超融合”,戴尔、Nutanix绝世好CP

从进入PC领域开始&#xff0c;戴尔一直在扮演颠覆者的角色。戴尔的理想是以开放、标准化的技术和解决方案颠覆传统的封闭的技术和市场&#xff0c;实现与合作伙伴的共赢。在超融合架构逐渐兴起的今天&#xff0c;戴尔依旧希望以变革者的身份&#xff0c;携手超融合架构的先驱Nu…

ruby array_Ruby中带有示例的Array.index()方法

ruby arrayArray.index()方法 (Array.index() Method) In this article, we will study about Array.index() method. You all must be thinking the method must be doing something which is related index of certain element. It is not as simple as it looks. Well, we w…

面试突击58:truncate、delete和drop的6大区别!

作者 | 磊哥来源 | Java面试真题解析&#xff08;ID&#xff1a;aimianshi666&#xff09;转载请联系授权&#xff08;微信ID&#xff1a;GG_Stone&#xff09;在 MySQL 中&#xff0c;使用 truncate、delete 和 drop 都可以实现表删除&#xff0c;但它们 3 个的使用场景和执行…

scala 去除重复元素_Scala程序从列表中删除重复项

scala 去除重复元素List in Scala is a collection that stores data in the form of a liked-list. The list is an immutable data structure but may contain duplicate elements. And in real life implementation duplicate elements increase the runtime of the program…

智力游戏

【Description】whitecloth 最近迷上了一个你小时候已经玩厌了的游戏&#xff1a;移火柴棒。他现在吵着要你陪他玩&#xff0c;你没有办法&#xff0c;只好写一个程序来完成这个工作了。你被给出了一个火柴拼成的等式&#xff0c;比如说下面这个&#xff1a;&#xff08; 5 7 …

面渣逆袭:MySQL六十六问!建议收藏

基础MySQ Logo作为SQL Boy&#xff0c;基础部分不会有人不会吧&#xff1f;面试也不怎么问&#xff0c;基础掌握不错的小伙伴可以跳过这一部分。当然&#xff0c;可能会现场写一些SQL语句&#xff0c;SQ语句可以通过牛客、LeetCode、LintCode之类的网站来练习。1. 什么是内连接…

C ++中带有示例的llabs()函数

C llabs()函数 (C llabs() function) llabs() function is a library function of cstdlib header. It used to get the absolute of the given value. This function is similar to the abs() and labs() functions except for the type of the parameter, it is used for th…

Mysql+Heartbeat+Drbd生产环境高可用部署若干问题解惑

MysqlHeartbeatDrbd生产环境高可用部署若干问题解惑&#xff1a;############################################################## Purpose: MysqlHeartbeatdrbd高可用部署中学生的几个疑惑解答## USER YYYY-MM-DD – ACTION # Oldboy 2011-3-14 – Created# …

try-with-resources 中的一个坑,注意避让

小伙伴们好呀&#xff0c;昨天复盘以前做的项目&#xff08;大概有一年了&#xff09;&#xff0c;看到这个 try-catch &#xff0c;又想起自己之前掉坑的这个经历 &#xff0c;弄了个小 demo 给大家感受下~ &#x1f604;问题1一个简单的下载文件的例子。这里会出现什么情况…

c++ abort 函数_C ++中带有示例的abort()函数

c abort 函数C abort()函数 (C abort() function) abort() function is a library function of cstdlib header. It is used to abort the current process. For the abnormal program termination – we can use abort() function. abort()函数是cstdlib标头的库函数。 用于中…