Python Programming
 
Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
User Name:
Password:
Remember me

The Shed is going Social! Join us on FaceBook and Twitter and chime in on the conversation.

Go Back   Dev Shed ForumsProgramming LanguagesPython Programming

Reply
Add This Thread To:
  Del.icio.us   Digg   Google   Spurl   Blink   Furl   Simpy   Y! MyWeb 
Thread Tools Search this Thread Rate Thread Display Modes
 
Unread Dev Shed Forums Sponsor:
  #1  
Old November 5th, 2012, 04:03 AM
ferdi.supernova ferdi.supernova is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 ferdi.supernova User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 3 sec
Reputation Power: 0
Run linux/unix command order by list of file

Hi,
I newbie on python.
Can we run unix/linux command order by list of file, which the file contains many name of file on some directory ?

Reply With Quote
  #2  
Old November 5th, 2012, 06:26 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,759 Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 38 m 3 sec
Reputation Power: 1568
If I am following you correctly, then the function you want is system(), which is in the os package.

Code:
import os

os.system("ls")


What this does is call out to the operating system shell and request that it run the other program, then return to the Python program when it is done.

Now, mind you, there are other ways to do this effectively. You can, for example, use the os.listdir() function, which gives the directory listing as a list of strings. From within a Python program, that one is probably the more useful, since you than can do what you want with that data.
__________________
Rev First Speaker Schol-R-LEA;2 JAM LCF ELF KoR KCO BiWM TGIF
#define KINSEY (rand() % 7) λ Scheme is the Red Pill
Scheme in ShortUnderstanding the C/C++ Preprocessor
Taming PythonA Highly Opinionated Review of Programming Languages for the Novice, v1.1

FOR SALE: One ShapeSystem 2300 CMD, extensively modified for human use. Includes s/w for anthro, transgender, sex-appeal enhance, & Gillian Anderson and Jason D. Poit clone forms. Some wear. $4500 obo. tverres@et.ins.gov

Reply With Quote
  #3  
Old November 5th, 2012, 08:25 PM
ferdi.supernova ferdi.supernova is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Nov 2012
Posts: 2 ferdi.supernova User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 19 m 3 sec
Reputation Power: 0
Quote:
Originally Posted by Schol-R-LEA
If I am following you correctly, then the function you want is system(), which is in the os package.

Code:
import os

os.system("ls")


What this does is call out to the operating system shell and request that it run the other program, then return to the Python program when it is done.

Now, mind you, there are other ways to do this effectively. You can, for example, use the os.listdir() function, which gives the directory listing as a list of strings. From within a Python program, that one is probably the more useful, since you than can do what you want with that data.


Hi, thanks for your comment..
I've made my script python like this

#!/usr/bin/env python
import subprocess

file="/home/ferdi/test_.txt"

f=open(file,'r')
for read_test in f :
data=f.readline()
test=subprocess.call("cd /home/ferdi/another;ls -l " + data,shell=True)
print test

which file contains
1.gif
2.gif
3.gif
4.gif

I want my result is like this :

-rwxrwxrwx. 1 ferdi ferdi 2985 Oct 31 02:09 1.gif
-rwxrwxrwx. 1 ferdi ferdi 2085 Oct 31 02:09 2.gif
-rwxrwxrwx. 1 ferdi ferdi 2185 Oct 31 02:09 3.gif
-rwxrwxrwx. 1 ferdi ferdi 2285 Oct 31 02:09 4.gif

Still error..
How to use this on python... ? Can you help me ?

Reply With Quote
  #4  
Old November 6th, 2012, 02:18 AM
Schol-R-LEA's Avatar
Schol-R-LEA Schol-R-LEA is offline
Commie Mutant Traitor
Dev Shed Intermediate (1500 - 1999 posts)
 
Join Date: Jun 2004
Location: Norcross, GA (again)
Posts: 1,759 Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level)Schol-R-LEA User rank is General 9th Grade (Above 100000 Reputation Level) 
Time spent in forums: 1 Month 2 Weeks 2 Days 3 h 38 m 3 sec
Reputation Power: 1568
First off, I should mention that the forum software does not retain indentation by default. In order to make your code readable, you need to put it in [code] tags, as I have here below:
Code:
#!/usr/bin/env python
import subprocess

file="/home/ferdi/test_.txt"

f=open(file,'r')
for read_test in f :
        data=f.readline()
        test=subprocess.call("cd /home/ferdi/another;ls -l " + data,shell=True)
        print test


As for changing the working directory, you might be better off using os.chdir("/home/ferdi/another"), before beginning the for: loop. You would then use subprocess.Popen() rather than subprocess.call(), and you would follow it immediately with a call to communicate().
Code:
import subprocess
import os

def listing():
    ls = subprocess.Popen("ls -l", stdout=subprocess.PIPE)
    return ls.communicate()[0]

ls = listing().decode()
print (ls)

lines = ls.split(os.linesep)

for line in lines:
    print(line)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Run linux/unix command order by list of file

Developer Shed Advertisers and Affiliates



Thread Tools  Search this Thread 
Search this Thread:

Advanced Search
Display Modes  Rate This Thread 
Rate This Thread:


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

vB code is On
Smilies are On
[IMG] code is On
HTML code is Off
View Your Warnings | New Posts | Latest News | Latest Threads | Shoutbox
Forum Jump

Forums: » Register « |  User CP |  Games |  Calendar |  Members |  FAQs |  Sitemap |  Support | 
  
 


Powered by: vBulletin Version 3.0.5
Copyright ©2000 - 2013, Jelsoft Enterprises Ltd.

© 2003-2013 by Developer Shed. All rights reserved. DS Cluster - Follow our Sitemap