0

Possible Duplicate:
Equivalent of Backticks in Python

I am looking for the best way to run a terminal command (ls -l) within Python. I have read about subprocess but I do not understand it fully, if someone could try and make me understand what is happening I would be grateful. I need to use the ls -l command to retrieve a hard link number which is != 1 and then save this number to match it against a directory number elsewhere. For now I would just like to know how to grab the hard link number and save it to a variable using subprocess (or a better method if there is one).

Here is the code I have used so far: #!/usr/bin/python

#tool that resolves time machine directories 

import os

#create output file 
os.chdir("/home/sean/Desktop")
hard_link_number = open('hardLinkNumber.log', 'w')

#move into mounted backup (figure out how to remove xe2 etc)
os.chdir("/mnt/Backups.backupdb/stuart dent\xe2\x80\x99s MacBook Pro/2010-08-10-160859/MAc")

#find hard link data 
print>>hard_link_number, os.system("ls -la")
hard_link_number.close()

os.system("ls -la") outputs the information I require but it will not save it to the file I have created. I read elsewhere that os.system will not output data.

Community
  • 1
  • 1
bigl
  • 1,063
  • 3
  • 13
  • 23

2 Answers2

6

You want os.stat (specifically the st_nlink attribute).

Edit: To paraphrase jwz: Some people, when confronted with a problem, think "I know, I'll parse the output of ls -l." Now they have two problems.

melpomene
  • 84,125
  • 8
  • 85
  • 148
2

You can pass the file object to stdout in subprocess.call() and the output will be saved to that file:

In [26]: import subprocess

In [27]: with open("data.txt","w") as f:
    subprocess.call("ls -la",stdout=f,shell=True)
   ....:   
Ashwini Chaudhary
  • 244,495
  • 58
  • 464
  • 504
  • 2
    You can't pass a `list` and have `shell=True` – GP89 Dec 14 '12 at 23:36
  • Try it, with and without `shell=True` – GP89 Dec 14 '12 at 23:47
  • @AshwiniChaudhary [Because](http://docs.python.org/2/library/subprocess.html#subprocess.Popen): [With `shell=True`] "If *args* is a sequence, the first item specifies the command string, and any additional items will be treated as additional arguments to the shell itself." – melpomene Dec 14 '12 at 23:48
  • 1
    Quoting the docs _The shell argument (which defaults to False) specifies whether to use the shell as the program to execute. If shell is True, it is recommended to pass args as a string rather than as a sequence._ – GP89 Dec 14 '12 at 23:49
  • `subprocess.check_output` is probably a simpler replacement for his existing code—it pretty much does exactly what he hoped `os.system` would, without having to understand `stdout` redirection or anything. Also, why use `shell=True` here? Instead of `os.system('ls -la')`, just `subprocess.check_output(['ls', '-la'])`, and nothing else needs to change. (Of course melpomene's answer is a better way to do what the OP actually _wants_.) – abarnert Dec 15 '12 at 01:02