LinuxQuestions.org
Review your favorite Linux distribution.
Home Forums Tutorials Articles Register
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices


Reply
  Search this Thread
Old 09-21-2012, 01:35 PM   #1
zippy4251
LQ Newbie
 
Registered: Mar 2005
Posts: 13

Rep: Reputation: 0
Help executing shell commands in a Python script


I am trying to create a Python script that would search recursively for a specific file extension and run a command on each individual file that is found. I did this in bash using a for loop that looked like this:

Code:
for files in $(find -type f -name "*.001")
do
     ftkimager $files --verify >> images.txt
done
ftkimager is a command line program for imaging hard drives. The script looks for .001 files, verifies against a MD5 hash, and places the results into images.txt

I wanted to let the user input a starting directory, so I added that to my Python script and use the subprocess module to run shell commands and execute ftkimager. Here is what I have so far for the Python script:

Code:
import subprocess

source = raw_input ("Enter the Path \n")

for files in subprocess.call (['find '+source+' -type f -name "*.001"], shell=True)
subprocess.call (['ftkimager '+source' --verify'], shell=True)
When run, I get an invalid syntax error on the for loop. Since the other parts work when I comment out the for statement, I'm assuming I'm doing the for loop incorrectly but I don't know how to get the command to work properly. Does anyone know how to fix this, or (if possible) do what I want to do in a better way?
 
Old 09-21-2012, 01:38 PM   #2
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Code:
for files in subprocess.call (['find '+source+' -type f -name "*.001"]', shell=True):
You forgot a closing quote.
 
Old 09-21-2012, 02:01 PM   #3
zippy4251
LQ Newbie
 
Registered: Mar 2005
Posts: 13

Original Poster
Rep: Reputation: 0
That fixed that! One step closer to success. Now I get the error "TypeError: 'int' object is not iterable" during the for loop. Now from what I'm gathering this means that "files" is not an integer, which is true. I'm pretty new to this so my thought may be way off here, but do I need to make "image" a string somehow?
 
Old 09-21-2012, 02:14 PM   #4
TobiSGD
Moderator
 
Registered: Dec 2009
Location: Germany
Distribution: Whatever fits the task best
Posts: 17,148
Blog Entries: 2

Rep: Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886Reputation: 4886
subprocess.call() will not return the output of the shell-command, but the exit code of that command, usually 0 if no error occured. Therefore the error-message that you can't iterate an int, simply because files is an int. If you want the actuall output have a look at either the stdout and stderr options of subprocess.call() or the subprocess.check_output() function: http://docs.python.org/library/subprocess.html

Last edited by TobiSGD; 09-21-2012 at 02:28 PM.
 
Old 09-21-2012, 02:19 PM   #5
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
You should also be using glob to generate a list of files to iterate over, not calling find.
 
Old 09-21-2012, 04:12 PM   #6
audriusk
Member
 
Registered: Mar 2011
Location: Klaipėda, Lithuania
Distribution: Slackware
Posts: 361

Rep: Reputation: 199Reputation: 199
I feel that shell scripts are better suited for tasks like this:
Code:
read -p 'Enter the path: ' dir
find $dir -type f -name '*.001' -print0 \
  | xargs -0 -I{} ftkimager {} --verify >> images.txt
 
Old 09-25-2012, 09:34 AM   #7
zippy4251
LQ Newbie
 
Registered: Mar 2005
Posts: 13

Original Poster
Rep: Reputation: 0
Thanks for all the help!

Quote:
If you want the actuall output have a look at either the stdout and stderr options of subprocess.call() or the subprocess.check_output() function: http://docs.python.org/library/subprocess.html
I'm looking more into this now to make things work how I want.

Quote:
You should also be using glob to generate a list of files to iterate over, not calling find.
The problem with glob is it is not recursive. I did find there's a module called glob2 that is recursive however.

Quote:
I feel that shell scripts are better suited for tasks like this:
Yep, I had a shell script that I posted first as an example that does work. I think that shell scripts are better suited for simple tasks, but I wanted to use it as a way to start learning Python. Necessity is the mother of invention and programming in my mind .

Since my original question was answered, I marked this as solved and will post any other questions in a new thread.
 
Old 09-25-2012, 11:20 AM   #8
dugan
LQ Guru
 
Registered: Nov 2003
Location: Canada
Distribution: distro hopper
Posts: 11,241

Rep: Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322Reputation: 5322
Quote:
Originally Posted by zippy4251 View Post
The problem with glob is it is not recursive. I did find there's a module called glob2 that is recursive however.
You could also combine fnmatch with os.walk. I would guess that this is how glob2 is implemented.

Last edited by dugan; 09-25-2012 at 11:26 AM.
 
  


Reply



Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off



Similar Threads
Thread Thread Starter Forum Replies Last Post
best way to automate shell commands - shell script or python scripts or something els parangsaraf Linux - Newbie 11 08-08-2012 06:17 PM
Executing commands through shell variable shriyer Linux - Software 2 07-23-2009 07:03 PM
GUI for executing shell commands shishirkotkar Linux - Newbie 1 04-12-2008 09:36 AM
executing shell commands in c++ true_atlantis Programming 2 10-01-2004 04:53 PM
python, executing regular linux commands Robert0380 Programming 3 06-26-2003 03:35 PM

LinuxQuestions.org > Forums > Non-*NIX Forums > Programming

All times are GMT -5. The time now is 05:42 PM.

Main Menu
Advertisement
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
Open Source Consulting | Domain Registration