öncelikle bir komutun ne işe yaradığını bilmiyorsak bunu terminalden öğrenebiliriz. mesela wc nin ne işe yaradığını bulmak için konsola man wc yazarız.Ve wc nin satır sayısını , kelime sayısını , byte sayısını gösterdiğini ögrenmiş olduk. bir de ayrıca -l , -c , -w gibi komutlar da çıktı. Şimdi bunları görelim :
-l => satır sayısını gösterir.
-c => byte sayısını gösterir.
Şimdi de cat komutunun ne işe yaradığına bakalım:
cat => cat komutu dosyları birletirdiği gibi adını yazdığımız dosyaların içeriklerini de yazdırır.
Şimdi artık cpu bilgilerini ögrenmeye başlayalım:
konsola
>> cat /proc/cpuinfo yazdıgımız zaman tum cpu bilgileri bize gösterilir.
daha az kısmını görmek istersek
>>less /proc/cpuinfo yazarız. Daha fazlası içinse
>>more /proc/cpuinfo yazarız. Bunlar bize gerekli gereksiz bilgileri de gösterir. Ama bizim amacımız içinde model geçen kelimeleri yazdırmak olsun. O halde yapacagımız şey
>> cat /proc/cpuinfo | grep model yazmaktır. Şimdi burdaki bilmedigimiz seyleri acıklayalım.
| ne anlama gelir ?? | den önce nerde işlem yapacagımızı yazarız. yani burda grep komutu /proc/cpuinfo kısmında işlem yapacak demektir. grep komutu ise içinde model gecen kelimeleri bize bulup gösterir.
PEKİ şimdiki görevimizde içinde model gecen kelime sayısını bulmak olsun.
>>cat /proc/cpuinfo | grep model | wc -l
8 output olarak 8 i alırız.demin de bahsettiğimiz gibi wc kelime satır bye sayısı gibi işlemlerin sonucu bulmamızı saglardı.burdaki -l ise kac satırda model kelimesinin gectigini bulmamızı saglar.
BİR de history komutu var.onunla da gecmişte kullandıgımız komutların cıktısını alırız. peki bu cıktı da kac tane python kelimesi geciyor bulmak istersek:
>> history | grep python | wc -l yazarız.
PEKİ bu cpu bilgilerini python kullanarak nasıl öğrenebiliriz??
deneme.py adlı bir python dosyası oluşturalım.
import subprocess
subprocess.call(["cat" , "/proc/cpuinfo"])
yazıp kaydettik. Şimdi terminalde nası calıstıracagımız kısmına gecelim:
>>python deneme.py | grep model
dedigimiz zaman aynı işlemi python kullanarak yapmış oluyoruz :)
Şimdilik bu kadar.....
23 Nisan 2015 Perşembe
bir programın linux ta nerde kurulu olduğunu python kullanarak bulma
bunu yapmak için subprocess diye bir kutuphaneyi programımıza eklememiz gerekiyor. bunu ypmak için programın en basına import subprocess dememiz yeterli.import bir kutuphaneyi kendi programımıza eklemek için kullanılır. yani subprocess i kullan demiş olduk. mesela firefox un nerde kurulu oldugunu bulmak istiyoruz. bunun için subprocess in call adlı komutunu kullanmamız lazım.call methodu pyhon da olmayan komutları kullanmamazı saglar.Gerekli kodu yazalım:
import subprocess
subprocess.call(["which","firefox"])
çalıştırdıgımızda aldıgımız sonuc:
>>/usr/bin/firefox
ve gorev tamamlanmış oldu...
import subprocess
subprocess.call(["which","firefox"])
çalıştırdıgımızda aldıgımız sonuc:
>>/usr/bin/firefox
ve gorev tamamlanmış oldu...
22 Nisan 2015 Çarşamba
subprocess nedir dersek?
nette şöyle bir acıklama buldum.The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions.
bi de soyle bi sey buldum:
A running program is called a process. Each process has its own system state, which includes memory, lists of open files, a program counter that keeps track of the instruction being executed, and a call stack used to hold the local variables of functions.
Normally, a process executes statements one after the other in a single sequence of control flow, which is sometimes called the main thread of the process. At any given time, the program is only doing one thing.
A program can create new processes using library functions such as those found in the os or subprocess modules such as os.fork(), subprocess.Popen(), etc. However, these processes, known as subprocesses, run as completely independent entities-each with their own private system state and main thread of execution.
Because a subprocess is independent, it executes concurrently with the original process. That is, the process that created the subprocess can go on to work on other things while the subprocess carries out its own work behind the scenes.
peki pipe nedir dersek?
Unix or Linux without pipes is unthinkable, or at least, pipelines are a very important part of Unix and Linux applications. Small elements are put together by using pipes. Processes are chained together by their standard streams, i.e. the output of one process is used as the input of another process. To chain processes like this, so-called anonomymous pipes are used.
nette şöyle bir acıklama buldum.The subprocess module allows you to spawn new processes, connect to their input/output/error pipes, and obtain their return codes. This module intends to replace several older modules and functions.
bi de soyle bi sey buldum:
A running program is called a process. Each process has its own system state, which includes memory, lists of open files, a program counter that keeps track of the instruction being executed, and a call stack used to hold the local variables of functions.
Normally, a process executes statements one after the other in a single sequence of control flow, which is sometimes called the main thread of the process. At any given time, the program is only doing one thing.
A program can create new processes using library functions such as those found in the os or subprocess modules such as os.fork(), subprocess.Popen(), etc. However, these processes, known as subprocesses, run as completely independent entities-each with their own private system state and main thread of execution.
Because a subprocess is independent, it executes concurrently with the original process. That is, the process that created the subprocess can go on to work on other things while the subprocess carries out its own work behind the scenes.
peki pipe nedir dersek?
Unix or Linux without pipes is unthinkable, or at least, pipelines are a very important part of Unix and Linux applications. Small elements are put together by using pipes. Processes are chained together by their standard streams, i.e. the output of one process is used as the input of another process. To chain processes like this, so-called anonomymous pipes are used.
21 Nisan 2015 Salı
Python da if döngüsü
python da if döngüsünü kısaca basit bi programla görelim.
number = int(raw_input("enter a number:"))
if number > 0:
print "Positive"
if number % 2 == 0:
print "even"
else:
print "odd"
elif number < 0:
print "negative"
else:
print "0"
her if ve else den sonra iki nokta koyuyoruz.
Şimdilik bu kadar :)
number = int(raw_input("enter a number:"))
if number > 0:
print "Positive"
if number % 2 == 0:
print "even"
else:
print "odd"
elif number < 0:
print "negative"
else:
print "0"
her if ve else den sonra iki nokta koyuyoruz.
Şimdilik bu kadar :)
Kaydol:
Kayıtlar (Atom)