ruby 关于目录操作

Ruby语言的文件操作 在Ruby中,文件操作非常简单且灵活。从创建和打开文件,到读取和写入数据,再到目录的操作和异常处理,Ruby都提供了清晰易用的方法。通过适当的异常处理,开发者可以确保程序的稳定性和可靠性。学习和掌握Ruby的文件操作,对于提升编程技能和开发效率具有重要意义。在实际开发中,我们经常需要处理配置文件、日志文件或用户上传的文件等场景,掌握这些操作将使工作变得更加高效。希望通过本文的介绍,读者能够全面了解Ruby中的文件操作,并在实际项目中灵活运用。 阅读详情

ruby 关于目录操作

版权声明:本文为博主原创文章,未经博主允许不得转载。
1.创建文件夹 
Dir.new %%1 
Dir::mkdir #不指定目录全名称时,缺省为工作目录 
Dir::chdir() 改变当前脚本工作目录 
FileUtils.mkdir 'test' 
file = File.new("cmd.txt") 
file.each do |line| 
puts line if line =~ /target/ 
end 

2.创建文件 
改变当前根目录 

Dir.chdir("/home/guy/sandbox/tmp")  
  
Dir.chroot("/home/guy/sandbox") 
Dir.new %%1 
#Dir::rmdir #不指定目录全名称时,缺省为工作目录 

3.删除文件 
改变当前根目录 
Dir.chdir("/home/guy/sandbox/tmp")  
Dir.chroot("/home/guy/sandbox") 
Dir.new %%1 
#Dir::rmdir #不指定目录全名称时,缺省为工作目录 

4.删除文件夹 
#require 'fileutils'  
FileUtils.rm_r(%%1) 

5.删除一个文件下夹所有的文件夹 
Dir::chdir 
Dir::pwd属性或者Dir.getwd() 
#改变当前脚本工作目录 

6.清空文件夹 
Dir::chdir %%1 改变当前脚本工作目录 
Dir::rmdir #不指定目录全名称时,缺省为工作目录 
Dir.new %%1 

#require 'ftools' 
FileUtils.mkdir 'test' 
file = File.new(%%1)  #"cmd.txt" 
file.each do |line| 
puts line if line =~ /target/ 
end 

7.读取文件 
#require 'ftools' 
File.open(%%1).readlines #'文件名' 

#require 'ftools' 
arr = IO.readlines(%%1)  #"myfile" 
lines = arr.size 
#puts "myfile has #{lines} lines in it." 
#longest = arr.collect {|x| x.length}.max 
#puts "The longest line in it has #{longest} characters." 


8.写入文件 
f=open(%%1,"w") 
f.puts(%%2) 

9.写入随机文件 
#require 'ftools' 
file = File.open(%%1,"w")  
file.seek(5)  
str = file.gets # "fghi" 

#require 'ftools' 
File.open('文件名') 
File.open("cmd.txt","r") do |file| 
while line=file.gets 
puts line 
end 
end 
puts 
file=File.new("cmd.txt","r") 
file.each_line do |line| 
puts line 
end 
IO.foreach("cmd.txt") do |line| 
puts line if line =~ /target/ 
puts line if line !~ /target/ 
end 
### 
Dir#pos 
返回当前子文件指针 

Dir#pos= 
设置子文件指针 

Dir#rewind 
设置子文件指针到起始位置 

Dir#seek 
设置子文件指针 

Dir#tell 
获取当前指针 

10.读取文件属性 
#文件中是否有内容,(返回false为有内容,返回true为空) 
File.new('文件名').stat.zero? 
#文件大小 
File.size?('文件名') 
flag1 = FileTest::zero?("file1") 
flag2 = FileTest::size?("file2") 
size1 = File.size("file1") 
size2 = File.stat("file2").size 
### 
File::atime(filename) 
返回指定文件的最后访问时间 

11.写入属性 

12.枚举一个文件夹中的所有文件夹 
#require 'ftools' 
puts Dir.glob('**/*').each { | file | file.downcase } 

#要区分目录和普通文件我们这样使用 
file1 = File.new("/tmp") 
file2 = File.new("/tmp/myfile") 
test1 = file1.directory? # true 
test2 = file1.file? # false 
test3 = file2.directory? # false 
test4 = file2.file? # true 
### 
遍历目录 
Dir.foreach(%%1) { |entry| puts entry} 

13.复制文件夹 
require "fileutils" 
FileUtils.cp %%1,%%2 

14.复制一个目录下所有的文件夹到另一个文件夹下 
#require 'FileUtils' 
list=Dir.entries(%%1) 
list.each_index do |x| 
FileUtils.cp "#{list[x]}",%%2 if !File.directory?(list[x]) 
end 

15.移动文件夹 
#require 'FileUtils' 
FileUtils.mv %%1,%%2 

16.移动一个目录下所有的文件夹到另一个目录下 
#require 'FileUtils' 
list=Dir.entries(%%1) 
list.each_index do |x| 
FileUtils.mv "#{list[x]}",%%2 if !File.directory?(list[x]) 

end


17.以一个文件夹的框架在另一个目录创建文件夹和空文件 
######################### 
//文件是否存在 
File.exist?('文件名') 
flag = FileTest::exist?("LochNessMonster") 
flag = FileTest::exists?("UFO") 
######################### 
require 'rubygems' 
require 'ruby-debug' 
require "find" 

module Cz_dirtools 

def mkdirs_to(tar,src=Dir.getwd) 
#debugger 
if tar.class==NilClass then 
puts "PLZ input target directory name..." 
return 
elsif !FileTest.directory?(tar)#如果tar不是一个目录 
puts ("Creating #{File.expand_path(tar)}") 
Dir.mkdir("#{File.expand_path(tar)}")#创建tar目录 
end 

src=if FileTest.directory?(src) then 
src#如果src是一个目录名,则返回自身 
else 
File.dirname(src)#如果src为一个文件名,则返回该文件所在目录 
end 
#Dir.foreach可以复制目标路径第一层目录结构而不复制子目录 
#~ Dir.foreach(src) do |dir| 
#~ if FileTest.directory?(dir) && dir!=tar && dir!='..' &&dir!='.' then #忽略自身,忽略上级目录"..",忽略本级目录"." 
#~ #puts dir 
#~ begin 
#~ Dir.mkdir("#{File.expand_path(tar)}/#{dir}") 
#~ rescue#如果该目录已存在则直接跳过否则创建该目录 
#~ end 
#~ end 
#~ end 
#Find.find可以复制目标路径的完整目录结构,包括子目录 
dirs=Array.new 
Find.find(src) do |dir| 
unless !FileTest.directory?(dir) || File.basename(dir)==tar || File.basename(dir)=='..' || File.basename(dir)=='.' || File.basename(dir)==File.basename(src)#忽略自身,忽略上级目录"..",忽略本级目录"." 
dirs<<dir 
end 
end 
#puts dirs 
dirs.each do |dir| 
begin 
Dir.mkdir("#{File.expand_path(tar)}/#{dir.gsub(src,'')}") 
rescue#如果该目录已存在则直接跳过否则创建该目录 
end 
end 
end 

end 

require 'rubygems' 
require 'ruby-debug' 
require "find" 

module Cz_dirtools 

def mkdirs_to(tar,src=Dir.getwd) 
#debugger 
if tar.class==NilClass then 
puts "PLZ input target directory name..." 
return 
elsif !FileTest.directory?(tar)#如果tar不是一个目录 
puts ("Creating #{File.expand_path(tar)}") 
Dir.mkdir("#{File.expand_path(tar)}")#创建tar目录 
end 

src=if FileTest.directory?(src) then 
src#如果src是一个目录名,则返回自身 
else 
File.dirname(src)#如果src为一个文件名,则返回该文件所在目录 
end 
#Dir.foreach可以复制目标路径第一层目录结构而不复制子目录 
#~ Dir.foreach(src) do |dir| 
#~ if FileTest.directory?(dir) && dir!=tar && dir!='..' &&dir!='.' then #忽略自身,忽略上级目录"..",忽略本级目录"." 
#~ #puts dir 
#~ begin 
#~ Dir.mkdir("#{File.expand_path(tar)}/#{dir}") 
#~ rescue#如果该目录已存在则直接跳过否则创建该目录 
#~ end 
#~ end 
#~ end 
#Find.find可以复制目标路径的完整目录结构,包括子目录 
dirs=Array.new 
Find.find(src) do |dir| 
unless !FileTest.directory?(dir) || File.basename(dir)==tar || File.basename(dir)=='..' || File.basename(dir)=='.' || File.basename(dir)==File.basename(src)#忽略自身,忽略上级目录"..",忽略本级目录"." 
dirs<<dir 
end 
end 
#puts dirs 
dirs.each do |dir| 
begin 
Dir.mkdir("#{File.expand_path(tar)}/#{dir.gsub(src,'')}") 
rescue#如果该目录已存在则直接跳过否则创建该目录 
end 
end 
end 

end 

18.复制文件 
#require 'FileUtils' 
FileUtils.cp %%1, %%2 

19.复制一个目录下所有的文件到另一个目录 
#require 'FileUtils' 
list=Dir.entries(%%1) 
list.each_index do |x| 
FileUtils.cp "#{list[x]}",%%2 if !File.directory?(list[x]) 
end 

20.提取扩展名 

21.提取文件名 
%%2 = File.basename(%%1) 

22.提取文件路径 
%%2 = File.dirname(%%1) 

23.替换扩展名 

24.追加路径 

25.移动文件 

26.移动一个文件夹下所有文件到另一个目录 
#require 'FileUtils' 
list=Dir.entries(%%1) 
list.each_index do |x| 
FileUtils.mv "#{list[x]}",%%2 if !File.directory?(list[x]) 
end 

27.指定目录下搜索文件 
#require "find"  
  
def findfiles(dir, name)  
 list = []  
 Find.find(dir) do |path|  
  Find.prune if [".",".."].include? path  
  case name  
   when String  
    list << path if File.basename(path) == name  
   when Regexp  
    list << path if File.basename(path) =~ name  
  else  
   raise ArgumentError  
  end  
 end  
 list  
end  
  
findfiles %%1,%%2 #"/home/hal", "toc.txt" 

28.打开对话框 

29.文件分割 
逐字节对文件进行遍历 

  可以使用each_byte方法,如果你想要转换byte到字符的话使用chr方法: 

file = File.new("myfile")  
e_count = 0  
file.each_byte do |byte|  
 e_count += 1 if byte == ?e  
end 

30.文件合并 
逐字节对文件进行遍历 

  可以使用each_byte方法,如果你想要转换byte到字符的话使用chr方法: 

file = File.new("myfile")  
e_count = 0  
file.each_byte do |byte|  
 e_count += 1 if byte == ?e  
end 


31.文件简单加密 

32.文件简单解密 

33.读取ini文件属性 

34.合并一个文件下所有的文件 

35.写入ini文件属性 

36.获得当前路径 
File.dirname($0) 

37.读取XML数据库 

38.写入XML数据库 

39.ZIP压缩文件 
#require 'rubygems'  
#require 'zip/zipfilesystem' 
Zip::ZipFile.open(%%1, Zip::ZipFile::CREATE) do |zip|   #'zipfile.zip' 
zip.file.open('file1', 'w') { |f| f << 'This is file 1.' }   
zip.dir.mkdir('sub_dir')   
zip.file.open('sub_dir/file2', 'w') { |f| f << 'This is file 2.' }   
end 

40.ZIP解压缩 

41.获得应用程序完整路径 

42.ZIP压缩文件夹 
#require 'rubygems'  
#require 'zip/zipfilesystem' 
def compress   
  Zip::ZipFile.open 'zipfile.zip', Zip::ZipFile::CREATE do |zip|   
    add_file_to_zip('dir', zip)   
  end  
end  
  
def add_file_to_zip(file_path, zip)   
  if File.directory?(file_path)   
    Dir.foreach(file_path) do |sub_file_name|   
      add_file_to_zip("#{file_path}/#{sub_file_name}", zip) unless sub_file_name == '.' or sub_file_name == '..'  
    end  
  else  
    zip.add(file_path, file_path)   
  end  
end  
add_file_to_zip %%1,%%2 

43.递归删除目录下的文件 
#require 'ftools' 
file_path = String.new 
file_path="D:" 
    if File.directory? file_path 
      Dir.foreach(file_path) do |file| 
        if file!="." and file!=".." 
puts "File:"+file 
        end 
      end 
    end 



44.验证DTD 

45.Schema 验证 

46.Grep 
#!/usr/bin/env ruby 
# Grep with full regexp-functionality via ruby 

if ARGV.shift == "-p" 
  pattern = Regexp.new(ARGV.shift) 
else 
  puts "Please give me a pattern with the '-p' option" 
  exit 
end 
ARGV.each do |filename| 
  File.open(filename) do |file| 
    file.each do |line| 
      puts "#{filename} #{file.lineno.to_s}: #{line}" if 
pattern.match(line) 
    end 
  end 
end 

Using it via: rgrep -p '/delete /i' *.php does not match anything, but 
this 
#!/usr/bin/env ruby 
# Grep with full regexp-functionality via ruby 

if ARGV.shift == "-p" 
  pattern = Regexp.new(ARGV.shift) 
else 
  puts "Please give me a pattern with the '-p' option" 
  exit 
end 
ARGV.each do |filename| 
  File.open(filename) do |file| 
    file.each do |line| 
      puts "#{filename} #{file.lineno.to_s}: #{line}" if /delete 
/i.match(line) 
    end 
  end 
end 

47.直接创建多级目录 
#require "fileutils" 
FileUtils.makedirs(%%1) 

48.批量重命名 

49.文本查找替换 ReplaceText 

50.文件关联

51.操作Excel文件 

52.设置JDK环境变量 

53.选择文件夹对话框 

54.删除空文件夹 

55.发送数据到剪贴板 

56.从剪贴板中取数据 

57.获取文件路径的父路径 

58.创建快捷方式 CreateShortCut 

59.弹出快捷菜单 


60.文件夹复制到整合操作 

61.文件夹移动到整合操作 

62.目录下所有文件夹复制到整合操作 

63.目录下所有文件夹移动到整合操作 

64.目录下所有文件复制到整合操作 

65.目录下所有文件移动到整合操作 

66.对目标压缩文件解压缩到指定文件夹 

67.创建目录副本整合操作 

68.打开网页 

69.删除空文件夹整合操作 

70.获取磁盘所有分区后再把光驱盘符去除(用"\0"代替),把结果放在数组allfenqu[] 中,数组中每个元素代表一个分区盘符,不包括 :\\ 这样的路径,allfenqu[]数组开始时存放的是所有盘符。 
当我用这样的代码测试结果是正确的,光驱盘符会被去掉: 
CString root; //root代表盘符路径 
for(i=0;i<20;i++) //0-20代表最大的盘符数 
{ 
root.Format("%c:\\",allfenqu[i]); 
if(GetDriveType(root)==5) 
allfenqu[i]='\0'; 
} 

但我用这样的代码时结果却无法去掉光驱盘符,allfenqu[]中还是会包含光驱盘符: 
CString root; 
for(i=0;i<20;i++) 
{ 
root=allfenqu[i]+":\\"; 
if(GetDriveType(root)==5) 
allfenqu[i]='\0'; 
} 

71.激活一个程序或程序关联的文件 

72.HTTP下载 

73.FTP下载 

74.写图像到剪切板 setClipboardImage 

75.从剪贴板复制图像到窗体 

22、Ruby 文件与目录操作全解析 本文深入解析了 Ruby 中文件与目录操作的各种技巧和方法,包括输入套接字的文件式操作、递归遍历目录树、文件锁定机制、生成版本化备份文件名、以及将字符串伪装成文件的技术。文章还提供了实际应用案例,如批量文件处理和多线程文件读写,并总结了最佳实践和注意事项,帮助开发者高效处理文件和目录相关任务。 阅读详情

相关推荐

52、Ruby中的哈希、集合、CSV和目录操作

本文深入介绍了Ruby中哈希、集合、CSV文件处理和目录操作的相关知识。内容涵盖哈希的键值转换技巧、集合的创建与运算、CSV文件的解析与写入,以及目录的搜索与管理操作。通过示例代码展示了这些功能在实际开发中的应用,并提供了使用建议与注意事项,帮助开发者更高效地进行数据处理和文件管理。

tcp8optimizer的博客 68

linux学习笔记

linux学习笔记 注意:下述内容仅是我的学习过程,是否具有普适性未经验证 文章目录linux学习笔记学习前的准备linux常用命令学习 学习前的准备 1、安装VMware虚拟机 2、下载linux系统光盘映像文件 3、安装远程管理工具SecureCRT和winSCP 4、开始配置虚拟机 5、开始安装linux系统 centOS的安装没有过多注意,按照提示进行即可。其中涉及挂载点的分配为\boot 200 \home 2000 swap 1000 \ 剩余空间 四个分区,我安装的是命令行(无图形界面)

m0_47114189的博客 642

Ruby编程:文件和目录操作的艺术

本文将带领读者深入了解Ruby语言中文件和目录操作的相关知识。从读取文件内容、写入数据、按行读取和遍历,到文件重命名、删除、权限更改,再到目录的浏览、创建、临时文件的生成,Ruby提供了强大的内置函数和方法,使得文件和目录的处理变得简单而高效。

weixin_30661119的博客 508

Ruby 创建目录

在进行导入或上传文件时需要创建临时目录以存放临时文件,这时就需要动态创建目录了。在windows中因为权限不那么严格,所以创建目录时可以 不指定目录的访问权限,但在linux环境下就不行了,只有正确的指定了目录的访问权限,才能正常地处理文件操作,否则可能出现权限不足的错误 file_path = "#{RAILS_ROOT}/public/tmp/csv" FileUtils.mkdir_...

weixin_34380296的博客 397

Ruby编程中文件和目录操作的实践指南

本文深入探讨了Ruby编程语言中处理文件和目录的多种方法,包括创建临时文件、使用内置函数对文件和目录进行操作,以及如何利用Dir类处理目录流。通过具体代码示例和类方法的详细解析,展示了Ruby强大的文件系统操作能力。...

weixin_35516624的博客 323

Ruby编程:文件与目录操作的深入探索

本文将深入探讨Ruby编程中处理文件和目录的核心方法。我们将从文件读写、目录遍历、文件权限修改以及创建和删除文件和目录等方面,结合实际示例代码,提供详细的解释和使用场景。通过本文的学习,读者将能够更加熟练地运用Ruby进行文件和目录的管理。...

weixin_35516624的博客 483

Ruby 文件目录操作(实例讲解更易懂)

File 操作 File::atime( path) 返回path的最后访问时间(access time)。 # Ruby [33] pry(main)> File::atime("hello.rb") => 2020-08-15 21:22:51 +0800 # 执行 cat hello.rb 后 # [root@master workspace]# cat hello.rb [34] pry(main)> File::atime("hello.rb") => 20..

Looking的网页备忘录和个人知识库 2361

Ruby异常处理与目录流操作的深度探索

本文深入探讨了Ruby编程语言中实例方法的使用,特别是在处理目录流和异常方面。通过实例展示了如何使用Dir类进行目录操作,以及如何通过begin/rescue/ensure/else/raise/catch等关键字优雅地处理异常情况,确保程序的健壮性和用户体验。...

weixin_35516624的博客 440

ruby 新建文件夹 fileutils

require 'fileutils' ROOT_DIR = '/home/zn/work/small-tools-master/price/' FileUtils.makedirs(ROOT_DIR) unless File.exists?ROOT_DIR 转载于:https://www.cnblogs.com/znsongshu/p/11371673.ht...

weixin_30701575的博客 811

c#源码,将矩阵(二维数组)渲染成云图,强度图,热力图,均匀性图,伪彩图

优快云软件0积分下载链接:https://download.youkuaiyun.com/download/wanghuannihao1992/86656759。6、渲染效果良好,颜色均匀分布,不存在等高线,交叉点。3、纯粹的C#代码编写,不含第三方dll或者控件。4、支持ColorBar显示,强度值颜色坐标显示。2、渲染时间60ms左右,速度快,1、二维数据最小支持2*2,7、采样全新数据插值算法。先直接上C#软件效果图。5、整图保存为单个图片。

业余程序员小徐的博客 6883

BIM Revit教程(六)尝试安装 Revit 时显示“Autodesk Revit 已安装。”

在安装Revit或RevitLT时,若系统提示“AutodeskRevit版本已安装”,通常是由于旧版本残留文件未完全清除所致。解决方案包括:1.通过控制面板或Windows设置卸载旧版本Revit,确保所有相关组件被移除;2.使用Autodesk卸载工具(AdskUninstallHelper)进一步清理;3.谨慎操作注册表,删除与Revit相关的残留项;4.确保系统有至少30GB的可用磁盘空间;5.完成上述步骤后,重新尝试安装Revit。此外,文章还提到了一个免费功能商店,供用户寻找提效工具,并鼓励粉丝

ZOZO_888的博客 2131

ruby生成html目录,ruby文件夹操作

一、新建文件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."...

weixin_35885813的博客 332

用户存续期价值评估(CLV) 一

用户存续期价值评估(CLV) 1 一. 定义 ​     用户存续期价值(Customer Lifetime Value),也可称为CLTV、LCV或者LTV,是企业对未来一个客户与其的关系所带来的净收益的预测。这个预测模型可以有不同水平的复杂度和精确度,从一个直观感觉到使用复杂的预测技术。CLV可以被简单理解为客户关系的价值,也就是未来由于与客户的关系所带...

weixin_43171270的博客 8027

Ruby on Rails 入门之:(23) Ruby 中文件,目录的操作

下面的程序简单的使用了文件目录的操作,仅作为抛砖引玉,供参考。具体的详细的功能还请参照API 程序中简单的使用了文件权限的读取,文件的读取,文件存在是否判断,获取文件路径,文件名称,获取目录路径,遍历一个目录的所有文件。 #encoding:utf-8 fp = "./test.rb"; mode = File.stat(fp).mode; puts "该文件的访问权

Work Hard, Play Harder! 2545

【Shader】Unity批量更换Shader

在Unity开发中,因为渲染管线问题,经常会遇到需要大量更改shader的问题,本篇文章讲述批量更换Shader的工具。

憨辰 1468

基于激光雷达的目标跟随功能移植到自己的ROS机器人小车上

提示:文章写完后,目录可以自动生成,如何生成可参考右边的帮助文档 文章目录前言一、下载目标跟随的源码二、修改话题名称1.修改/cmd_vel话题名称2.修改/scan话题名称3.修改双电机正反转4.目标跟随功能测试 前言 本人尝试把github上的一个目标跟随项目移植到自己的机器人小车上,并且成功了,最终小车可以跟着人移动,在这里分享一下心得。基于激光雷达的目标跟随,通过获取激光雷达的信息,跟随最近的物体。 提示:以下是本篇文章正文内容,下面案例可供参考 一、下载目标跟随的源码 源码地址:https:

weixin_44574633的博客 4463

DRV8818PWPR+STM32F446RE步进电机驱动系统设计与工业应用

步进电机驱动是工业自动化中实现精准定位的核心技术,其性能取决于驱动芯片的电流控制精度、微步细分能力与MCU的实时运动规划能力。DRV8818PWPR凭借2.5A持续输出、1/32硬件微步、可编程衰减模式及分级故障诊断机制,显著提升系统确定性;STM32F446RE则依托高级定时器互补PWM、双ADC同步采样和CAN FD/USB OTG接口,为闭环控制与工业互联提供坚实支撑。该组合在AGV转向、协作机器人关节、高精度3D打印等场景中,兼顾成本、EMC鲁棒性与微米级重复定位精度,是开环步进向类伺服性能演进的关

weixin_34248023的博客 322

python实现DoIP基本通信(收发报文)

python实现DoIP基本通信(收发报文)

天赐好车的博客 898

Ruby目录及文件操作

得到当前目录或者父目录操作如下 require 'pathname'puts Pathname.new(__FILE__).realpathputs Dir.pwdputs File.dirname(__FILE__)puts File.expand_path("..",Dir.pwd)puts File.expand_path(".",Dir.pwd)puts __FILE__puts Fil...

weixin_34178244的博客 241

如何在MacBook上安装部署原生openclaw

本文介绍了在MacBook上安装OpenClaw的详细步骤:首先通过curl或国内镜像源安装nvm和Node.js(版本≥22),然后使用nvm安装Node.js 24并验证安装。接着通过脚本快速安装OpenClaw,完成安装后选择Qwen作为AI模型并进行OAuth认证。配置过程中可跳过联网搜索和Agent技能设置,最后选择Web界面作为呈现方式。安装完成后,系统会启动openclaw-gateway进程并打开Web界面,用户可通过简单的"hello"指令开始与AI对话。目前基础功能已

木简熙的博客 810

Buck电源设计常见的一些问题(一)半桥栅极驱动芯片共性失效问题

半桥驱动芯片主要用在器件可应用于电信,数据通信,便携式存储或更低电压系统中的电源转换器,半桥、全桥、推挽、同步降压,正激变换器和同步整流器等方面。当然难免会遇到不少问题。基本上我们在产品开发的过程中都遇到过栅极驱动芯片坏掉的情况,所以找了一些资料,学习学习。也查找查找原因。

小幽余生不加糖 2371

ruby对文件和目录的基本操作

新建目录Dir.mkdir("/tmp/test") FileUtils.mkdir_p("/tmp/test1/test2") #建目录结构新建文件my_file = File.new("/tmp/abc.txt")读文件File.read("/tmp/abc.txt") 或 File.open("/tmp/abc.txt","r")常用的方法还有File.readlines,将文件按行

weixin_33953249的博客 400

IEM模型,地表粗糙度模拟

利用微波反演土壤水分时候,利用IEM模型可以实现对随机地表的模拟

开源大模型六大安全弱点与实战加固指南

大模型安全是AI工程化落地的关键基石。随着开源模型权重开放和本地部署增多,攻击面从单一API扩展到权重文件、推理框架、微调数据、RAG链路等多个环节,安全风险也随之升级。提示注入、越狱逃逸、训练数据投毒、隐私泄漏等漏洞,不仅影响模型输出质量,更可能引发数据泄露和合规事故。理解这些攻击原理,掌握针对性的防护与红队测试方法,是保障生产环境稳定运行的前提。本文从安全评估出发,系统梳理六大典型安全弱点,给出从输入过滤、输出审核到权限隔离的加固实践,为正在做本地部署、模型微调和RAG应用的团队提供一份可落地的自查手册

weixin_34080951的博客 427

文档:亲测可用解决vnc灰屏,黑屏,鼠标大黑叉情况

主要解决vnc-viewer灰屏,黑屏,鼠标大黑叉等现象,详细见文档。

jdk-8u144-linux-x64.tar.gz.

该资源为搭建Linux系统Java环境用,资源的后缀为tar.gz格式的压缩包

RUBY-文件、目录操作

#------------------------------------------- 试题描述 ----------------------------------------- # 试题背景: #     测试人员开发完自动化用例后经常要将用例归档到TMSS,归档时TMSS要求用例的编号唯一,即编号不能重复, #     但测试人员在写用例时喜欢拷贝用例,这样容易导致一些用例的编号出现

llvslc521的博客 456

深度学习工具包 DeepLearningToolbox

深度学习工具包Deprecation notice.-----This toolbox is outdated and no longer maintained.There are much better tools available for deep learning than this toolbox, e.g. [Theano](http://deeplearning.net/software/theano/), [torch](http://torch.ch/) or [tensorflow](http://www.tensorflow.org/)I would suggest you use one of the tools mentioned above rather than use this toolbox.Best, Rasmus.DeepLearnToolbox================A Matlab toolbox for Deep Learning.Deep Learning is a new subfield of machine learning that focuses on learning deep hierarchical models of data.It is inspired by the human brain's apparent deep (layered, hierarchical) architecture.A good overview of the theory of Deep Learning theory is[Learning Deep Architectures for AI](http://www.iro.umontreal.ca/~bengioy/papers/ftml_book.pdf)For a more informal introduction, see the following videos by Geoffrey Hinton and Andrew Ng.* [The Next Generation of Neural Networks](http://www.youtube.com/watch?v=AyzOUbkUf3M) (Hinton, 2007)* [Recent Developments in Deep Learning](http://www.youtube.com/watch?v=VdIURAu1-aU) (Hinton, 2010)* [Unsupervised Feature Learning and Deep Learning](http://www.youtube.com/watch?v=ZmNOAtZIgIk) (Ng, 2011)If you use this toolbox in your research please cite [Prediction as a candidate for learning deep hierarchical models of data](http://www2.imm.dtu.dk/pubdb/views/publication_details.php?id=6284)```@MASTERSTHESIS\{IMM2012-06284, author = "R. B. Palm", title = "Prediction as a candidate for learning deep hierarchical models of data", year = "2012",}```Contact: rasmusbergpalm at gmail dot comDirectories included in the toolbox-----------------------------------`NN/` - A library for Feedforward Backpropagation Neural Networks`CNN/` - A library for Convolutional Neural Networks`DBN/` - A library for Deep Belief Networks`SAE/` - A library for Stacked Auto-Encoders`CAE/` - A library for Convolutional Auto-Encoders`util/` - Utility functions used by the libraries`data/` - Data used by the examples`tests/` - unit tests to verify toolbox is workingFor references on each library check REFS.mdSetup-----1. Download.2. addpath(genpath('DeepLearnToolbox'));Example: Deep Belief Network---------------------```matlabfunction test_example_DBNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit RBM and visualize its weightsrand('state',0)dbn.sizes = [100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);figure; visualize(dbn.rbm{1}.W'); % Visualize the RBM weights%% ex2 train a 100-100 hidden unit DBN and use its weights to initialize a NNrand('state',0)%train dbndbn.sizes = [100 100];opts.numepochs = 1;opts.batchsize = 100;opts.momentum = 0;opts.alpha = 1;dbn = dbnsetup(dbn, train_x, opts);dbn = dbntrain(dbn, train_x, opts);%unfold dbn to nnnn = dbnunfoldtonn(dbn, 10);nn.activation_function = 'sigm';%train nnopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.10, 'Too big error');```Example: Stacked Auto-Encoders---------------------```matlabfunction test_example_SAEload mnist_uint8;train_x = double(train_x)/255;test_x = double(test_x)/255;train_y = double(train_y);test_y = double(test_y);%% ex1 train a 100 hidden unit SDAE and use it to initialize a FFNN% Setup and train a stacked denoising autoencoder (SDAE)rand('state',0)sae = saesetup([784 100]);sae.ae{1}.activation_function = 'sigm';sae.ae{1}.learningRate = 1;sae.ae{1}.inputZeroMaskedFraction = 0.5;opts.numepochs = 1;opts.batchsize = 100;sae = saetrain(sae, train_x, opts);visualize(sae.ae{1}.W{1}(:,2:end)')% Use the SDAE to initialize a FFNNnn = nnsetup([784 100 10]);nn.activation_function = 'sigm';nn.learningRate = 1;nn.W{1} = sae.ae{1}.W{1};% Train the FFNNopts.numepochs = 1;opts.batchsize = 100;nn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.16, 'Too big error');```Example: Convolutional Neural Nets---------------------```matlabfunction test_example_CNNload mnist_uint8;train_x = double(reshape(train_x',28,28,60000))/255;test_x = double(reshape(test_x',28,28,10000))/255;train_y = double(train_y');test_y = double(test_y');%% ex1 Train a 6c-2s-12c-2s Convolutional neural network %will run 1 epoch in about 200 second and get around 11% error. %With 100 epochs you'll get around 1.2% errorrand('state',0)cnn.layers = { struct('type', 'i') %input layer struct('type', 'c', 'outputmaps', 6, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %sub sampling layer struct('type', 'c', 'outputmaps', 12, 'kernelsize', 5) %convolution layer struct('type', 's', 'scale', 2) %subsampling layer};cnn = cnnsetup(cnn, train_x, train_y);opts.alpha = 1;opts.batchsize = 50;opts.numepochs = 1;cnn = cnntrain(cnn, train_x, train_y, opts);[er, bad] = cnntest(cnn, test_x, test_y);%plot mean squared errorfigure; plot(cnn.rL);assert(er<0.12, 'Too big error');```Example: Neural Networks---------------------```matlabfunction test_example_NNload mnist_uint8;train_x = double(train_x) / 255;test_x = double(test_x) / 255;train_y = double(train_y);test_y = double(test_y);% normalize[train_x, mu, sigma] = zscore(train_x);test_x = normalize(test_x, mu, sigma);%% ex1 vanilla neural netrand('state',0)nn = nnsetup([784 100 10]);opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samples[nn, L] = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.08, 'Too big error');%% ex2 neural net with L2 weight decayrand('state',0)nn = nnsetup([784 100 10]);nn.weightPenaltyL2 = 1e-4; % L2 weight decayopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex3 neural net with dropoutrand('state',0)nn = nnsetup([784 100 10]);nn.dropoutFraction = 0.5; % Dropout fraction opts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex4 neural net with sigmoid activation functionrand('state',0)nn = nnsetup([784 100 10]);nn.activation_function = 'sigm'; % Sigmoid activation functionnn.learningRate = 1; % Sigm require a lower learning rateopts.numepochs = 1; % Number of full sweeps through dataopts.batchsize = 100; % Take a mean gradient step over this many samplesnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex5 plotting functionalityrand('state',0)nn = nnsetup([784 20 10]);opts.numepochs = 5; % Number of full sweeps through datann.output = 'softmax'; % use softmax outputopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, train_x, train_y, opts);[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');%% ex6 neural net with sigmoid activation and plotting of validation and training error% split training data into training and validation datavx = train_x(1:10000,:);tx = train_x(10001:end,:);vy = train_y(1:10000,:);ty = train_y(10001:end,:);rand('state',0)nn = nnsetup([784 20 10]); nn.output = 'softmax'; % use softmax outputopts.numepochs = 5; % Number of full sweeps through dataopts.batchsize = 1000; % Take a mean gradient step over this many samplesopts.plot = 1; % enable plottingnn = nntrain(nn, tx, ty, opts, vx, vy); % nntrain takes validation set as last two arguments (optionally)[er, bad] = nntest(nn, test_x, test_y);assert(er < 0.1, 'Too big error');```[![Bitdeli Badge](https://d2weczhvl823v0.cloudfront.net/rasmusbergpalm/deeplearntoolbox/trend.png)](https://bitdeli.com/free "Bitdeli Badge")

上一篇: Ruby语言学习系列--基本的ruby语法
下一篇: 存储过程
普通网友
博客等级 码龄11年 52粉丝 12原创
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符  | 博主筛选后可见
 
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

个

红包个数最小为10个

元

红包金额最低5元

当前余额3.43元 前往充值 >
需支付:10.00元
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付元
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值