Python / subprocess> Symbolic link Implementation to get only the destination file name> os.readlink ()

Operating environment


Xeon E5-2620 v4 (8 cores) x 2
32GB RAM
CentOS 6.8 (64bit)
openmpi-1.8.x86_64 and its-devel
mpich.x86_64 3.1-5.el6 and its-devel
gcc version 4.4.7 (And gfortran)
NCAR Command Language Version 6.3.0
WRF v3.7.Use 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

I want to get the original link file of LN_link_170324, test_original_170324, from a Python script.

Reference Get the return value of an external shell script (ls) with python3 Reference 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)

result


$ python test_python_170324j.py 
test_original_170324

What you taught me

(Addition 2017/03/25)

@ komeda-shinji told me os.readlink () in Comment.

Thank you for the information.

Recommended Posts

Python / subprocess> Symbolic link Implementation to get only the destination file name> os.readlink ()
I want to get the file name, line number, and function name in Python 3.4
[python] Change the image file name to a serial number
Change the standard output destination to a file in Python
Try to decipher the garbled attachment file name with Python
Python> library> os> os.walk ()> Get directory structure / Implementation to get each file path in the specified directory
Link to get started with python
How to get the Python version
Get the host name in Python
[Python Kivy] How to get the file path by dragging and dropping
[Python] Get the variable name with str
Get the path to the systemd unit file
The file name was bad in Python and I was addicted to import
[Python] Get the character code of the file
python I don't know how to get the printer name that I usually use.
[Python] I tried to get the type name as a string from the type function
Python Memorandum: Refer to the text and edit the file name while copying the target file
Extract only the file name excluding the directory in the directory
How to get the files in the [Python] folder
2017-02-19 Python> Link> Redirect to output file immediately> sys.stdout.flush ()
Get the update date of the Python memo file.
The only way to get iter_rows back to the beginning with openpyxl is to reload the file with load_workbook.
I want to see the file name from DataLoader
Template of python script to read the contents of the file
Get the file name in a folder using glob
How to get the number of digits in Python
[Python] Get the official file path of the shortcut file (.lnk)
I tried to touch the CSV file with Python
Read the xml file by referring to the Python tutorial
Python program that looks for the same file name
Get the URL of the HTTP redirect destination in Python
I want to change the symbolic link destination of / lib64 from / usr / lib64 to / my-lib64 on CentOS
Try to get the function list of Python> os package
How to switch the configuration file to be read by Python
Minimum knowledge to get started with the Python logging module
Get only the Python version (such as 2.7.5) on the CentOS 7 shell
Get the MIME type in Python and determine the file format
Get the file name saved in AWS S3 (1000 or more)
How to get the last (last) value in a list in Python
Debug by attaching to the Python process of the SSH destination
How to get into the python development environment with Vagrant
I tried to divide the file into folders with Python
[Introduction to Python] How to get data with the listdir function
To get the name of the primitive etc. generated immediately before
Get the source of the page to load infinitely with python.
Get the link destination URL by specifying a text sentence with Python scraping (Beautiful Soup) + XPath
Get the list in the S3 bucket with Python and search with a specific Key. Output the Key name, last update date, and count number to a file.