python:os

差分

このページの2つのバージョン間の差分を表示します。

この比較画面へのリンク

両方とも前のリビジョン 前のリビジョン
次のリビジョン
前のリビジョン
python:os [2016/10/30 23:05] – [パーミッションの取り扱い] mumeiyamibitopython:os [2019/04/25 11:06] (現在) – [更新日時] mumeiyamibito
行 1: 行 1:
-====== Python モジュール OS ======+====== Python モジュール: os ======
 ===== 概要 ===== ===== 概要 =====
   * ファイルシステムを利用するためのモジュール   * ファイルシステムを利用するためのモジュール
行 9: 行 9:
  
 ==== ファイルの存在 ==== ==== ファイルの存在 ====
-  * 存在の確認\\ <code python>+=== 存在の確認 === 
 +<code python>
 if os.path.exists("hoge.txt"): if os.path.exists("hoge.txt"):
  print("file exist")  print("file exist")
行 15: 行 16:
  print("No such file")  print("No such file")
 </code> </code>
-  * ファイルであるかの確認\\ <code python>+ 
 +=== ファイルであるかの確認 === 
 +<code python>
 if os.path.isfile("hoge.txt"): if os.path.isfile("hoge.txt"):
  print("hoge.txt is file")  print("hoge.txt is file")
行 21: 行 24:
  print("No such file")  print("No such file")
 </code> </code>
-  * ディレクトリであるかの確認\\ <code python>+ 
 +=== ディレクトリであるかの確認 === 
 +<code python>
 if os.path.isdir("hoge.txt"): if os.path.isdir("hoge.txt"):
  print("hoge.txt is file")  print("hoge.txt is file")
行 27: 行 32:
  print("No such file")  print("No such file")
 </code> </code>
-  * リンクであるかの確認\\ <code python>+ 
 +=== リンクであるかの確認 === 
 +<code python>
 if os.path.islink("hoge.txt"): if os.path.islink("hoge.txt"):
  print("hoge.txt is file")  print("hoge.txt is file")
行 34: 行 41:
 </code> </code>
  
-==== パーミッションの取り扱い ====+==== パーミッション ====
   * パーミッションを取得 (Linux のマスク形式で取得)\\ <code python>   * パーミッションを取得 (Linux のマスク形式で取得)\\ <code python>
-oct(os.stat("hoge.txt")[ST_MODE][-4:]+oct(os.stat("hoge.txt").st_mode)[-4:]
 </code> </code>
-通常の 3 桁の前に ''0'' が付いた形式で返される (例: 0755, 0600, ...)+    * 通常の 3 桁の前に ''0'' が付いた形式で返される (例: 0755, 0600, ...)
     * 参考サイト: [[http://stackoverflow.com/questions/5337070/how-can-i-get-a-files-permission-mask | python - How can I get a file's permission mask? - Stack Overflow]]     * 参考サイト: [[http://stackoverflow.com/questions/5337070/how-can-i-get-a-files-permission-mask | python - How can I get a file's permission mask? - Stack Overflow]]
   * パーミッションの設定 (Linux のマスク形式で設定)\\ <code python>   * パーミッションの設定 (Linux のマスク形式で設定)\\ <code python>
行 45: 行 52:
     * 今回はパーミッションを 0755 に設定している     * 今回はパーミッションを 0755 に設定している
     * 通常のパーミッションの 3 桁の前に ''0'' を付ける上、この数字は 8 進数なので、''0o'' を付ける     * 通常のパーミッションの 3 桁の前に ''0'' を付ける上、この数字は 8 進数なので、''0o'' を付ける
 +    * 参考サイト: [[http://stackoverflow.com/questions/15607903/python-module-os-chmodfile-664-does-not-change-the-permission-to-rw-rw-r-bu | Python module os.chmod(file, 664) does not change the permission to rw-rw-r-- but -w--wx---- - Stack Overflow]]
       * ''0o'' を付けないと Syntax error (SyntaxError: invalid token) になる       * ''0o'' を付けないと Syntax error (SyntaxError: invalid token) になる
 +      * 参考サイト: [[http://stackoverflow.com/questions/1837874/invalid-token-when-using-octal-numbers | python - Invalid Token when using Octal numbers - Stack Overflow]]
 +
 +==== 更新日時 ====
 +  * ''os.stat'' で ''os.stat_result'' オブジェクトを取得し整形する。\\ <code python>
 +os.stat(FILE).st_atime
 +os.stat(FILE).st_mtime
 +os.stat(FILE).st_ctime
 +</code>
 +    * ''FILE'': 更新日時を取得するファイル
 +    * ''.st_atime'': 最終アクセス日時
 +    * ''.st_mtime'': 最終内容更新日時
 +    * ''.st_ctime'': メタデータ最終更新日時
 +    * 得られる日時は UNIX 時間 (1970 年 1 月 1 日 0 時 0 分 0 秒からの時間) なので、datetime モジュールを用いて変換する。\\ <code python>
 +from datetime import datetime
 +datetime.fromtimestamp(os.stat(FILE).st_atime).strftime("%Y/%m/%d %H:%M:%S")
 +datetime.fromtimestamp(os.stat(FILE).st_mtime).strftime("%Y/%m/%d %H:%M:%S")
 +datetime.fromtimestamp(os.stat(FILE).st_ctime).strftime("%Y/%m/%d %H:%M:%S")</code>
 +  * 参考サイト: [[https://note.nkmk.me/python-os-stat-file-timestamp/ | Pythonでファイルのタイムスタンプ(作成日時や更新日時)を取得 | note.nkmk.me]]
 +
 +
 +==== 環境変数取得 ====
 +  * シェル内部での環境変数を取得する方法\\ <code python>VARIABLE = os.environ[ENV_VAR]</code>
 +    * ''VARIABLE'': シェル変数を受け取るための変数
 +    * ''ENV_VAR'': 環境変数名 (文字列)
 +    * 例: \\ <code python>hoge = os.environ["PATH"]</code>
 +    * nautilus-scripts や nemo-scripts の変数も環境変数であるので、この方法で取得する
 +  * 参考サイト: [[http://palepoli.skr.jp/tips/nautilus/script.php | Nautilus をスクリプトで拡張 - L'Isola di Niente]]
 +
 +==== リンク先を取得 ====
 +  * [[https://siguniang.wordpress.com/2009/11/25/%E3%82%B7%E3%83%B3%E3%83%9C%E3%83%AA%E3%83%83%E3%82%AF%E3%83%AA%E3%83%B3%E3%82%AF%E5%85%88%E3%81%AE%E3%83%91%E3%82%B9%E3%82%92%E5%8F%96%E5%BE%97/ | シンボリックリンク先のパスを取得 | Siguniang's Blog]]
 +
 +{{tag>プログラミング}}
 +
  • python/os.1477836346.txt.gz
  • 最終更新: 2016/10/30 23:05
  • by mumeiyamibito