LoginSignup
32
18

More than 5 years have passed since last update.

nimファイル操作

Last updated at Posted at 2016-05-21

概要

nimのファイル操作関連処理をコツコツと書いていく予定

以下メモ

テキストをファイルに書き込む

import os,times
block:
  var f : File = open("members.txt" ,FileMode.fmWrite)
  defer :
    close(f)
    echo "closed"
  f.writeLine "Mr.スポック"
  f.writeLine "Mr.ビーン"
(stdout)
closed

テキストファイルを読み込む

import os,times
block:
  var f : File = open("members.txt" , FileMode.fmRead)
  defer :
    close(f)
    echo "closed"
  echo f.readLine()
  echo f.readLine()
  #echo f.readLine()
(stdout)
Mr.スポック
Mr.ビーン
closed

テキストファイルをEOFまで読み込む

import os,times
block:
  var f : File = open("members.txt" , FileMode.fmRead)
  defer :
    close(f)
    echo "closed"
  while f.endOfFile == false :
    echo f.readLine()
  #echo f.readLine()
(stdout)
Mr.スポック
Mr.ビーン
closed

テキストファイルをすべて読み込む

import os,times
block:
  var f : File = open("members.txt" , FileMode.fmRead)
  defer :
    close(f)
    echo "closed"
  echo f.readAll()
  #echo f.readLine()
(stdout)
Mr.スポック
Mr.ビーン

closed

ファイルサイズを取得する

import os,times
block:
  var f : File = open("members.txt")
  defer :
    close(f)
    echo "closed"
  echo f.getFileSize
  # こちらでも同じ
  echo getFileSize("members.txt")
  echo "members.txt".getFileSize()
(stdout)
29
29
29
closed

ファイルの日付を取得

import os,times
block:
  # 日付を利用するには、timesモジュールのimportが必要
  var ctime,mtime,ltime : Time
  ctime = getCreationTime("members.txt")
  mtime = getLastModificationTime("members.txt")
  ltime = getLastAccessTime("members.txt")
  echo ctime
  echo mtime
  echo ltime
(stdout)
Wed May 25 22:04:53 2016
Wed May 25 22:04:53 2016
Wed May 25 22:04:53 2016

ファイルの存在確認

import os,times
block:
  echo os.existsFile("members.txt")
(stdout)
true

パスの結合

import os,times
block:
  echo joinPath("/","tmp","log") == "/tmp" / "log"
(stdout)
true

パスの分割

import os,times
block:
  var parent = splitPath("usr/local/bin")
  echo parent.head
  echo parent.tail
block:
  var parent = "usr/local/bin".splitPath
  echo parent.head
  echo parent.tail
(stdout)
usr/local
bin
usr/local
bin

親ディレクトリを取得し、パスを連結

import os,times
block:
  var path = "/usr/local/bin" /../ "lib"
  echo path
(stdout)
/usr/local/lib

ファイル名の分解

import os,times
block:
  var (dir, name, ext) = splitFile("usr/local/nimc.html")
  echo dir
  echo name
  echo ext
(stdout)
usr/local
nimc
.html

パターンにマッチするファイルを取得する

import os,times
block:
  var i = 0
  for f in walkFiles("/usr/bin/z*"):
    i += 1 ; if i > 10: break # 数が多いので10個まで
    echo f
(stdout)
/usr/bin/zcat
/usr/bin/zcmp
/usr/bin/zdiff
/usr/bin/zegrep
/usr/bin/zeisstopnm
/usr/bin/zenheiset
/usr/bin/zenity
/usr/bin/zfgrep
/usr/bin/zforce
/usr/bin/zgrep

ディレクトリ内のファイルを取得する

import os,times
block:
  var i = 0
  for f in walkDir("/var/log"):
    i += 1 ; if i > 10: break # 数が多いので10個まで
    # タプルが返される
    echo f.path
(stdout)
/var/log/tallylog
/var/log/lastlog
/var/log/openlmi-install.log
/var/log/wtmp
/var/log/sssd
/var/log/samba
/var/log/httpd
/var/log/ppp
/var/log/pluto
/var/log/libvirt

再帰的にディレクトリを探索する

import os,times
block:
  var i = 0
  for f in walkDirRec("/var/log/"):
    i += 1 ; if i > 10: break # 数が多いので10個まで
    echo "recursive:" & f
(stdout)
recursive:/var/log/tallylog
recursive:/var/log/lastlog
recursive:/var/log/openlmi-install.log
recursive:/var/log/wtmp
recursive:/var/log/yum.log
recursive:/var/log/dmesg.old
recursive:/var/log/Xorg.0.log.old
recursive:/var/log/wpa_supplicant.log-20160309
recursive:/var/log/spooler-20160503
recursive:/var/log/secure-20160521

再帰的にディレクトリを探索する+ディレクトリのみ表示

import os,times
block:
  # と思ったら、pcDirではyieldしないみたい
  var i = 0
  for f in walkDirRec("/var/log/",{pcDir}):
    i += 1 ; if i > 10: break # 数が多いので10個まで
    echo "dir only:" & f
(stdout)

再帰的にディレクトリを探索する+ファイルのみ表示

import os,times
block:
  var i = 0
  for f in walkDirRec("/var/log/",{pcFile}):
    i += 1 ; if i > 10: break # 数が多いので10個まで
    echo "file only:" & f
(stdout)
file only:/var/log/tallylog
file only:/var/log/lastlog
file only:/var/log/openlmi-install.log
file only:/var/log/wtmp
file only:/var/log/yum.log
file only:/var/log/dmesg.old
file only:/var/log/Xorg.0.log.old
file only:/var/log/wpa_supplicant.log-20160309
file only:/var/log/spooler-20160503
file only:/var/log/secure-20160521

テンポラリディレクトリを取得

import os,times
block:
  echo getTempDir()
(stdout)
/tmp/

アプリケーション関連フォルダ

import os,times
block:
  # アプリケーションファイル名
  echo getApplicationFileName()
  echo getAppFileName()
  # アプリケーション実行フォルダ
  echo getApplicationDir()
  echo getAppDir()
(stdout)
/tmp/test
/tmp/test
/tmp
/tmp

2つのファイルのうちどちらが新しいか判定

import os,times
block:
  open("a.txt",FileMode.fmWrite).close
  sleep(1000)
  open("b.txt",FileMode.fmWrite).close
  # a is newer than b => false
  echo fileNewer("a.txt","b.txt")
  # b is newer than a => true
  echo fileNewer("b.txt","a.txt")
(stdout)
false
true

2つのファイルが同じかどうかをチェックする

import os,times
block:
  echo sameFileContent("members.txt","members.txt")
  echo sameFileContent("members.txt","a.txt")
(stdout)
true
false
32
18
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
32
18