Here's a summary of the ways to call external programs and the advantages and disadvantages of each:
os.system("some_command with args")passes the command and arguments to your system's shell. This is nice because you can actually run multiple commands at once in this manner and set up pipes and input/output redirection. For example,
os.system("some_command < input_file | another_command > output_file")
However, while this is convenient, you have to manually handle the escaping of shell characters such as spaces, etc. On the other hand, this also lets you run commands which are simply shell commands and not actually external programs.
stream = os.popen("some_command with args")will do the same thing as
os.systemexcept that it gives you a file-like object that you can use to access standard input/output for that process. There are 3 other variants of popen that all handle the i/o slightly differently. If you pass everything as a string, then your command is passed to the shell; if you pass them as a list then you don't need to worry about escaping anything.
Popenclass of the
subprocessmodule. This is intended as a replacement for
os.popenbut has the downside of being slightly more complicated by virtue of being so comprehensive. For example, you'd say
print subprocess.Popen("echo Hello World", shell=True, stdout=subprocess.PIPE).stdout.read()
print os.popen("echo Hello World").read()
but it is nice to have all of the options there in one unified class instead of 4 different popen functions.
callfunction from the
subprocessmodule. This is basically just like the
Popenclass and takes all of the same arguments, but it simply waits until the command completes and gives you the return code. For example:
return_code = subprocess.call("echo Hello World", shell=True)
If you're on Python 3.5 or later, you can use the new subprocess.run function, which is a lot like the above but even more flexible and returns a CompletedProcess object when the command finishes executing.
The os module also has all of the fork/exec/spawn functions that you'd have in a C program, but I don't recommend using them directly.
subprocess module should probably be what you use.
Finally please be aware that for all methods where you pass the final command to be executed by the shell as a string and you are responsible for escaping it there are serious security implications if any part of the string that you pass can not be fully trusted (for example if a user is entering some/any part of the string). If unsure only use these methods with constants. To give you a hint of the implications consider this code
print subprocess.Popen("echo %s " % user_input, stdout=PIPE).stdout.read()
and imagine that the user enters "my mama didnt love me && rm -rf /".
- boot repair
- acetone iso
- free file sync
-Numix >> KDE Stuff
-4k flat material
wallpaper -> material design
qtcurve -> numix
color scheme -> material
window decoration qtcurve
cursor -> oxygen neon
Desktop theme -> arc dark
Python Threading Örneği:
#!/usr/bin/env python2 import threading import time class MyThread (threading.Thread): def __init__(self, thread_id, name, some_more_variable): threading.Thread.__init__(self) self.thread_id = thread_id self.name = name self.some_more_variable = some_more_variable self.daemon = True # Ana islem sonlandiginda thread'de sonlanacak def run(self): # thread islemi pass # Yeni threadler yarat thread1 = MyThread(1, "Thread-1","other_variable") thread2 = MyThread(2, "Thread-2","other_variable") # Yeni threadleri baslat thread1.start() thread2.start() sleep(50) # Ana thread. Buraya kod gelecek # threadler bir sekilde kesilirse sonlandırıldığını garantilememiz gerekiyor thread1.join() thread2.join()
- run(): thread.start() ile tetiklenen fonksiyon. Asıl işi yapan method burası
- start(): thread.start() , multhreading başlatır ve run() methodunu çağırır.
- join([n]): n saniye sonra thread'i sonlandırır
- isAlive(): thread hala çalışıyormu kontrol eder
- getName(): thread'in adını döndürür
- setName(): thread'in adını set eder
Fastest way to construct a string :
text = "%d,C100,%s,,%s,V101,%s,0,1,#" % (self.message_counter, str(self.device_id), time_str, data_list))
Rounding a float:
'%.5f' % .12345678
Read file and split by lines :
with open("test.txt", "a") as myfile: myfile.write("appended text")
|r||Opens a file for reading only. The file pointer is placed at the beginning of the file. This is the default mode.|
|rb||Opens a file for reading only in binary format. The file pointer is placed at the beginning of the file. This is the default mode.|
|r+||Opens a file for both reading and writing. The file pointer placed at the beginning of the file.|
|rb+||Opens a file for both reading and writing in binary format. The file pointer placed at the beginning of the file.|
|w||Opens a file for writing only. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.|
|wb||Opens a file for writing only in binary format. Overwrites the file if the file exists. If the file does not exist, creates a new file for writing.|
|w+||Opens a file for both writing and reading. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.|
|wb+||Opens a file for both writing and reading in binary format. Overwrites the existing file if the file exists. If the file does not exist, creates a new file for reading and writing.|
|a||Opens a file for appending. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.|
|ab||Opens a file for appending in binary format. The file pointer is at the end of the file if the file exists. That is, the file is in the append mode. If the file does not exist, it creates a new file for writing.|
|a+||Opens a file for both appending and reading. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.|
|ab+||Opens a file for both appending and reading in binary format. The file pointer is at the end of the file if the file exists. The file opens in the append mode. If the file does not exist, it creates a new file for reading and writing.|
Disk Erişim Hızını İyileştirme:
Dikteki dosyaların her okunduğunda bir de okundu bilgisinin yazılmaması için noatime kullanılır(nodiratime da içerir). Journal 'i kapatmak için data=writeback, kullanılır.
/etc/fstab >> UUID=7f5392f8-939b-4149-9f04-8b377ad0cdb4 / ext4 defaults,noatime,data=writeback,errors=remount-ro 0 1 # /tmp dizinini RAM'e bağlama tmpfs /tmp tmpfs defaults,noatime,nosuid,nodev,noexec,mode=1777,size=512M 0 0
/etc/sysctl.conf >> #Swap kullanim oranini azaltir vm.swappiness=10 # Inode kullanan sistem nesnelerinin daha uzun sure cache de kalmarini saglar vm.vfs_cache_pressure=50
TTY Sayısını 3 e İndirmek:
/etc/default/console-setup >> ACTIVE_CONSOLES=”/dev/tty[1-3]
sudo rm /etc/init/tty6.conf /etc/init/tty5.conf /etc/init/tty4.conf
Preload İle Sık Kullanılan Programları Önyüklemek:
sudo apt-get install preload
Hybernate Ve Sleep i kinit 'de başlatılmasını engellemek:
# Başına # koyarak yorum satırı haline getir: # RESUME=UUID=427075a3-381d-466b-a8d4-d08e8d183b6c
dpkg --get-selections | grep -v deinstall # list installed files apt-get clean # clean apt cache apt-get clean all # clean apt cache apt-get autoremove # clean unneeded files
Could not find climate_control-0.0.3 in any of the sources (Bundler::GemNotFound)
bundle install --path vendor/cache
#!/bin/bash echo -ne '##### (33%)\r' sleep 1 echo -ne '############# (66%)\r' sleep 1 echo -ne '####################### (100%)\r' echo -ne '\n'
\r : Satırın başına gitmeye yarıyor
-n : '\n' yi yazdırma
-e : '\r' gibi ifadeleri yakalamak için