0

How can I display the open file descriptors of a process in linux using python script? I tried using

readlink /proc/PID/fd/* 

in python script but I am getting error.

/bin/sh: 2: /fd/*: not found

I think subprocess module will not give error. How will I use it to remove error if possible to display file descriptors?

Code:

import os
p=os.popen("pgrep -x vlc")
q=p.read()
print("Process ID of VLC : ",q)
process= os.popen("readlink /proc/"+str(q)+"/fd/*")
s=process.read()
print(s)

closed as off-topic by Romeo Ninov, user88036, maulinglawns, Thomas, RalfFriedl Sep 23 '18 at 12:02

  • This question does not appear to be about Unix or Linux within the scope defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

  • worth adding the remaining output from the script, e.g. that "Process ID of VLC : " output. – steve Sep 22 '18 at 19:12
  • Process ID will keep changing everytime vlc is opened. So we can assume output as "Process ID of VLC : 12699" – ironman Sep 22 '18 at 19:17
  • Surely popen is for opening a single file. Use of * is bogus ? – steve Sep 22 '18 at 19:49
  • You are right but code is giving error. When I run only the "readlink /proc/12699/fd/*" I am not getting error. – ironman Sep 22 '18 at 19:50
2

You'll have to strip the trailing newline from q, ex. q = q.strip().

Also, you'll have to think through what happens if there's more than one vlc running.

  • I'm curious to know , how did you find that? I spent lot of time in figuring out this error. But all effort was in vain. – ironman Sep 23 '18 at 4:53

Not the answer you're looking for? Browse other questions tagged or ask your own question.