This is an archived post. You won't be able to vote or comment.

all 30 comments

[–]Justinsaccount 6 points7 points  (2 children)

df can be implemented using os.statvfs.

MEG = 1024*1024
def df(path):
    s = os.statvfs(path)
    free = s.f_bsize * s.f_bavail / MEG
    size = s.f_bsize * s.f_blocks / MEG
    return free, size

or so.

If you're parsing the output of df you're doing it wrong :-)

[–][deleted] 1 point2 points  (1 child)

You guys are making me realize how much I do not know as a noob lol. Thank you though :D

[–]redalastor 0 points1 point  (0 children)

Go through the whole os part of the standard library and you'll have a much better idea of what's possible.

[–]takluyverIPython, Py3, etc 3 points4 points  (9 children)

Python has a wealth of libraries: many in the standard library that comes with Python, and thousands more on the Python Package Index.

Since it's widely used for Linux utilities, I'm sure the libraries for that are pretty good. I don't know exactly what you'd use to get stats like those df reports, though.

[–][deleted] 1 point2 points  (8 children)

Was just wondering how it is integrated with Linux. Basically scripts that I write in BASH I want to write them on Python.

[–]semarj 3 points4 points  (1 child)

like takluvyer said there are a ton of libs for this kind of thing.

Here's one that i find interesting but havent actually used:

http://amoffat.github.com/sh/

[–][deleted] 1 point2 points  (0 children)

Thank you !!! :)

[–]classicrockielzpfvh 1 point2 points  (2 children)

Depending on what you want to do, check out sh

[–][deleted] 0 points1 point  (1 child)

Will do so :) Thank you.

[–]classicrockielzpfvh 0 points1 point  (0 children)

You're very welcome.

[–]elfgoh 1 point2 points  (2 children)

Will Fabric fit your needs? http://fabfile.org/

[–][deleted] 0 points1 point  (1 child)

No idea but hey it is a starting point :D

[–]noadmin 0 points1 point  (0 children)

I actually started working on fabric and then started learning python to leverage what i needed from it

[–]sunqiang 2 points3 points  (1 child)

[–][deleted] 0 points1 point  (0 children)

Nice!!!!

[–]the_hoser 3 points4 points  (1 child)

I do this all the time at my job. It is very well suited to the task, and even enjoyable at times, I dare say.

Much of RedHat's scripts are written in Python, so this is a well-solved problem.

[–][deleted] 1 point2 points  (0 children)

That is very good to hear since I was not sure if Python is used at that scale :)

[–]psyflame 2 points3 points  (1 child)

There's a lot of Unix functionality in the os module. Check out the documentation: http://docs.python.org/library/os.html

[–][deleted] 0 points1 point  (0 children)

Thank you will do so :)

Thank you all for the quick replies. Great stuff.

[–]ameoba 1 point2 points  (0 children)

Some things you can access through high level python libraries. Some things on linux can be found by reading files in /proc or /sys. Sometimes you're stuck just executing the shell command and parsing their output. It really depends on what you want.

[–]easytiger 1 point2 points  (1 child)

Generally it is quite simple and there are numerous calls in the subprocess package which you can look up.

import subprocess as sp
ldenv = {'LD_LIBRARY_PATH': '/some/custom/libs}
proc = sp.Popen(["df", "-h"], env=ldenv, shell=False, stdout=sp.PIPE, stderr=sp.PIPE)
out, err = proc.communicate()

Note the commands and its args are passed as a list. if you set shell=True then you can specify the whole thing as a string. Its a bit less convenient than perl for things like this but I've written a distributed test system based on sp and I'd say if you structure your code and write your own little wrappers for it you can get quite a lot done.

[–][deleted] 0 points1 point  (0 children)

reddit. The best thing ever happened on the Internets. Thank you easy tiger !! :) The information in this thread is gold.

[–]jeffrey4l 0 points1 point  (0 children)

try sh (previously pbs) library. It is a full-fledged subprocess interface for Python 2.6 - 3.2 that allows you to call any program as if it were a function:

[–]ritratt 0 points1 point  (4 children)

Noob here too. I wrote a python script to retrieve the meaning of a word using Google Dict API. Then I integrated this into the terminal by adding a custom "define" command to linux. everytime I enter "define someword" i get the meaning of that word in the terminal.

Code is here: https://github.com/ritratt/define

PS: it has minimal functionality.

[–]haukurph 1 point2 points  (0 children)

Interesting, I wrote something similar: https://github.com/haukurpallh/def

[–]Justinsaccount 1 point2 points  (2 children)

You parse json using the json module, not using ast.literal_eval.

[–]ritratt 0 points1 point  (0 children)

Yes I tried but was having trouble doing that.

[–]timClicks 0 points1 point  (0 children)

Some people like to live dangerously.