LoginSignup
2

More than 5 years have passed since last update.

Python / subprocess > Symbolic link先のファイル名だけ取得する実装 > os.readlink()を教えていただきました

Last updated at Posted at 2017-03-24
動作環境
Xeon E5-2620 v4 (8コア) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 とその-devel
mpich.x86_64 3.1-5.el6とその-devel
gcc version 4.4.7 (とgfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.1を使用。
Python 3.6.0 on virtualenv
$ touch test_original_170324
$ ln -fs test_original_170324 LN_link_170324
$ ls -l LN_link_170324 
lrwxrwxrwx 1 user user 20 Mar 24 16:06 LN_link_170324 -> test_original_170324

LN_link_170324の元リンクファイルであるtest_original_170324をPython scriptから取得したい。

参考 python3で外部のシェルスクリプト(ls)とかの返り値を取得
参考 https://docs.python.jp/3/library/subprocess.html

test_python_170324j.py
from subprocess import getoutput

# Python 3.6.0 on virtualenv

cmd = 'ls -l LN_link_170324'
last = getoutput(cmd).split(' ')[-1]
print(last)
結果
$ python test_python_170324j.py 
test_original_170324

教えたいただいた事項

(追記 2017/03/25)

@komeda-shinjiさんにコメントにてos.readlink()を教えていただきました。

情報感謝です。

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
2