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 January 11th, 2013, 04:21 AM
mik3nl mik3nl is offline
Registered User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jan 2013
Posts: 1 mik3nl User rank is Just a Lowly Private (1 - 20 Reputation Level) 
Time spent in forums: 48 m 13 sec
Reputation Power: 0
Output ls|awk to dict question

Hello,

I'm hoping someone might be able/willing to point me in the right direction to combat a small challenge I’m facing.
I'm pretty new to Python and already went thoroughly through "Learning Python (4th.ed)" and though this forum, but still not able to achieve the following:

I'm trying to create a dictionary in python3 based on the output of an awk'd ls command: ls -l | grep - | awk {'print $9","$5'}

The output from os.system("ls -l /bin | grep - | awk {'print $9\"=\" $5\",\"'}") seems to me to be the first step, but how to convert this to a dict is where i'm stuck..


Thanks for any pointers you might have!

Regards,
Mike.

Reply With Quote
  #2  
Old January 11th, 2013, 09:01 AM
SuperOscar SuperOscar is offline
Contributing User
Dev Shed Newbie (0 - 499 posts)
 
Join Date: Jul 2007
Location: Joensuu, Finland
Posts: 404 SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level)SuperOscar User rank is Second Lieutenant (5000 - 10000 Reputation Level) 
Time spent in forums: 1 Week 5 h 6 m 48 sec
Reputation Power: 65
Quote:
Originally Posted by mik3nl
I'm trying to create a dictionary in python3 based on the output of an awk'd ls command: ls -l | grep - | awk {'print $9","$5'}


If I’m getting you right, the “grep -” part is there to get rid of the header line, $9 is the filename and $5 the file’s size. (BTW, you are relying on a specific formatting of ls’s output. If ls happens to be aliased or the localisation in use differs, for example, the field numbers might change.)

I think the smart way is to use Python all the way through:

Code:
import os
fileinfo = {}
for filename in os.listdir('.'):
    fileinfo[filename] = os.stat(filename).st_size


Later on you could replace the '.' parameter with the contents of sys.argv, for instance.
Comments on this post
b49P23TIvg agrees: Great answer. mik3nl script fails when file name contains space.
__________________
My armada: openSUSE 12.3 (home desktop, laptop, work desktop), Ubuntu 12.04 LTS (mini laptop), Debian GNU/Linux 7.0 (server), Mythbuntu 12.04 LTS (HTPC), Bodhi Linux 2.0 & Windows 7 Ultimate (test desktop), FreeBSD 9.1 (test server)

Reply With Quote
Reply

Viewing: Dev Shed ForumsProgramming LanguagesPython Programming > Output ls|awk to dict question

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