ruby文件夹操作

Python014

ruby文件夹操作,第1张

一、新建文件

f=File.new(File.join("C:","Test.txt"), "w+")

f.puts("I am Jack")

f.puts("Hello World")

文件模式

"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 length or creates a new file for reading and writing.

"a" :Write-only. Starts at end of file if file existsotherwise, creates a new file for writing.

"a+" :Read-write. Starts at end of file if file existsotherwise, creates a new file for reading and writing.

"b" :(DOS/Windows only.) Binary file mode. May appear with any of the key letters listed above

二、读取文件

file=File.open(File.join("C:","Test.txt"),"r")

file.each { |line| print "#{file.lineno}.", line }

file.close

三、新建、删除、重命名文件

File.new( "books.txt", "w" )

File.rename( "books.txt", "chaps.txt" )

File.delete( "chaps.txt" )

四、目录操作

1     创建目录

Dir.mkdir("c:/testdir")

04     #删除目录

05     Dir.rmdir("c:/testdir")

07     #查询目录里的文件

08     p Dir.entries(File.join("C:","Ruby")).join(' ')

10     #遍历目录

11     Dir.entries(File.join("C:","Ruby")).each {

|e| puts e

}

1、ARGV and ARGF

ARGV

ARGV <<"cnblogslink.txt"

#The gets method is a Kernel method that gets lines from ARGV

print while gets

p ARGV.class

ARGF

while line = ARGF.gets

print line

end

2、文件信息查询

#文件是否存在

p File::exists?( "cnblogslink.txt" ) # =>true

#是否是文件

p File.file?( "cnblogslink.txt" ) # =>true

#是否是目录

p File::directory?( "c:/ruby" ) # =>true

p File::directory?( "cnblogslink.txt" ) # =>false

#文件权限

p File.readable?( "cnblogslink.txt" ) # =>true

p File.writable?( "cnblogslink.txt" ) # =>true

p File.executable?( "cnblogslink.txt" ) # =>false

#是否是零长度

p File.zero?( "cnblogslink.txt" ) # =>false

#文件大小 bytes

p File.size?( "cnblogslink.txt" ) # =>74

p File.size( "cnblogslink.txt" ) # =>74

#文件或文件夹

p File::ftype( "cnblogslink.txt" ) # =>"file"

#文件创建、修改、最后一次存取时间

p File::ctime( "cnblogslink.txt" ) # =>Sat Sep 19 08:05:07 +0800 2009

p File::mtime( "cnblogslink.txt" ) # =>Sat Sep 19 08:06:34 +0800 2009

p File::atime( "cnblogslink.txt" ) # =>Sat Sep 19 08:05:07 +0800 2009

3、查找文件

puts "查找目录下所有文件及文件夹"

Dir["c:/ruby/*"].each {|x|

puts x

}

puts "条件查询"

Dir.foreach('c:/ruby') {

|x| puts x if x != "." &&x != ".."

}

puts "查找某一类型文件"

Dir["*.rb"].each {|x|

puts x

}

puts "Open 查询"

Dir.open('c:/ruby') { |d| d.grep /l/ }.each{|x| puts x}

puts "---------------------------"

puts "正则表达式查询"

Dir["c:/ruby/ruby/[rs]*"].each{|x| puts x}

puts "------------------------"

Dir["c:/ruby/[^s]*"].each{|x| puts x}

puts "------------------------"

Dir["c:/ruby/{ruby,li}*"].each{|x| puts x}

puts "------------------------"

Dir["c:/ruby/?b*"].each{|x| puts x}

puts "查找目录及子目录的文件"

require 'find'

Find.find('./') { |path| puts path }

3、查询目录及子目录文件

require "find"

Find.find("/etc/passwd", "/var/spool/lp1", ".") do |f|

Find.prune if f == "."

puts f

end

原型:ref.find( [ aName ]* ) {| aFileName | block }

prune:Skips the current file or directory, restarting the loop with the next entry. If the current file is a directory, that directory will not be recursively entered. Meaningful only within the block associated with Find::find.

4、文件比较 复制等

require 'ftools'

File.copy 'testfile', 'testfile1'  » true

File.compare 'testfile', 'testfile1'  » true

Python是一种跨平台的计算机程序设计语言。是一个高层次的结合了解释性、编译性、互动性和面向对象的脚本语言。最初被设计用于编写自动化脚本(shell),随着版本的不断更新和语言新功能的添加,越多被用于独立的、大型项目的开发。

Python的创始人为荷兰人吉多·范罗苏姆(GuidovanRossum)。1989年圣诞节期间,在阿姆斯特丹,Guido为了打发圣诞节的无趣,决心开发一个新的脚本解释程序,作为ABC语言的一种继承。

之所以选中Python(大蟒蛇的意思)作为该编程语言的名字,是取自英国20世纪70年代首播的电视喜剧《蒙提.派森的飞行马戏团》(MontyPython'sFlyingCircus)。

扩展资料:

python中文就是蟒蛇的意思。在计算机中,它是一种编程语言。Python(英语发音:/ˈpaɪθən/),是一种面向对象、解释型计算机程序设计语言,由GuidovanRossum于1989年底发明,第一个公开发行版发行于1991年。Python语法简洁而清晰,具有丰富和强大的类库。

它常被昵称为胶水语言,它能够把用其他语言制作的各种模块(尤其是C/C++)很轻松地联结在一起。常见的一种应用情形是,使用Python快速生成程序的原型(有时甚至是程序的最终界面),然后对其中有特别要求的部分,用更合适的语言改写。

比如3D游戏中的图形渲染模块,性能要求特别高,就可以用C++重写。1发展历程编辑自从20世纪90年代初Python语言诞生至今,它逐渐被广泛应用于处理系统管理任务和Web编程。Python已经成为最受欢迎的程序设计语言之一。

参考资料:

百度百科-Python

安装Xcode 4.1,Xcode4.2以及更高的版本在 Lion 仍然存在一些兼容性问题,强烈建议使用XCode 4.1,下载地址:

安装RVM

$ bash <<(curl -s )

配置RVM自动加载,将下面这一行代码添加到~/.bash_profile中,然后退出iTerm并重新启动

[[ -s $HOME/.rvm/scripts/rvm ]] &&source $HOME/.rvm/scripts/rvm

安装 ruby-1.9.2-p290

$ rvm install 1.9.2

设置系统默认使用 ruby-1.9.2

$ rvm use 1.9.2 --default

步骤1 安装 Rails

安装Rails

$ gem install rails

Rails安装完成后,创建一个rails项目,假定你的项目叫做:awesome project

$ rails new awesome_project

启动Rails,并访问

$ cd awesome_project $ rails server

步骤2 安装 Passenger 和 Nginx

首先通过gem安装passenger

$ gem install passenger

因为Nginx不支持动态module载入,所以需要通过Passenger来自动下载,编译,安装由Passenger修改版的Nginx:

安装Passenger + Nginx

$ passenger-install-nginx-module

Yes: download, compile and install Nginx for me. (recommended) The easiest way to get started. A stock Nginx 1.0.10 with Passenger support, but with no other additional third party modules, will be installed for you to a directory of your choice.

No: I want to customize my Nginx installation. (for advanced users) Choose this if you want to compile Nginx with more third party modules besides Passenger, or if you need to pass additional options to Nginx's 'configure' script. This installer will 1) ask you for the location of the Nginx source code, 2) run the 'configure' script according to your instructions, and 3) run 'make install'.

Whichever you choose, if you already have an existing Nginx configuration file, then it will be preserved.

Enter your choice (1 or 2) or press Ctrl-C to abort: 这里建议选择1

Please specify a prefix directory [/opt/nginx]: /usr/local/nginx

当询问nginx的安装路径的时候,个人建议安装到/usr/local/nginx下

当安装完成后,会在console中提示如何配置Nginx

Passenger会自动帮你将下面两行添加到Nginx的配置文件中/usr/local/nginx/conf/nginx.conf(很人性化)

http { ... passenger_root /Users/Daniel/.rvm/gems/ruby-1.9.2-p290/gems/passenger-3.0.10passenger_ruby /Users/Daniel/.rvm/wrappers/ruby-1.9.2-p290/ruby... }

server { listen 80server_name www.yourhost.comroot /somewhere/public# <--- be sure to point to 'public'! passenger_enabled on}

请不要忘记将nginx命令行程序连接到/usr/local/sbin

$ sudo ln -s /usr/local/nginx/sbin/nginx /usr/sbin/

步骤3 配置Nginx + Passenger + Rails

修改hosts文件,给你的项目一个本地域名, 比如awesome_project.local

$ sudo vim /etc/hosts

127.0.0.1 awesome_project.local

测试hosts

$ ping awesome_project.local

PING awesome_project.local (127.0.0.1): 56 data bytes

64 bytes from 127.0.0.1: icmp_seq=0 ttl=64 time=0.054 ms

继续配置Nginx, 这里我给出一个最小可运行的Nginx配置文件

$ vim /usr/local/nginx/conf/nginx.conf

nginx.conf

worker_processes 1

events {

worker_connections 1024

}

http {

passenger_root /Users/Daniel/.rvm/gems/ruby-1.9.2-p290/gems/passenger-3.0.10

passenger_ruby /Users/Daniel/.rvm/wrappers/ruby-1.9.2-p290/ruby

include mime.types

default_type application/octet-stream

sendfile on

keepalive_timeout 65

server {

listen 80

server_name awesome_project.local

root /Users/Daniel/awesome_project/public

passenger_enabled on

rails_env development

}

}

测试Nginx的配置文件语法是否正确

$ sudo nginx -t

nginx: the configuration file /usr/local/nginx/conf/nginx.conf syntax is ok

nginx: configuration file /usr/local/nginx/conf/nginx.conf test is successful

启动Nginx

$ sudo nginx

如何在修改Nginx的配置文件后,让Nginx载入新配置

$ sudo nginx -s reload

如何停止Nginx

$ sudo nginx -s stop

如何在不停Nginx的情况下,重新启动Passenger

$ cd path/to/your/awesome/project

$ touch tmp/restart.txt

好了,这个时候你可以打开浏览器,访问你的awesome_project网站了