python: info général ******************** idle ==== pour le lancer .. code-block:: bash C:\Python31\python.exe C:\Python31\Lib\idlelib\idle.pyw -o %1 %2 %3 %4 %5 %6 %7 %8 %9 C:\Python31\python.exe C:\Python31\Lib\idlelib\idle.pyw -h installation d’un module ======================== .. code-block:: python python setup.py install lecture de l’entrée standart ============================ .. code-block:: python reponse = raw_input("Votre age: ") Comment quitter son application ? ================================= .. code-block:: python import sys sys.exit() Comment lancer un programme externe ? ===================================== .. code-block:: python import os os.startfile('c:/windows/notepad.exe') os.startfile('MonImage.bmp') os.system("c:/windows/notepad.exe monfichier.log") os.system('MonImage.bmp') #nouveau processus os.spawnl(os.P_NOWAIT, "c:/windows/notepad.exe") os.popen("c:/windows/notepad.exe monfichier.log") autre solution .. code-block:: python import commands commands.getstatusoutput('ls -l') pour connaitre le retour d'une commande .. code-block:: python import os a = os.popen("ping") a.read() Comment ne pas afficher la fenêtre DOS à l’exécution du programme ? =================================================================== Si vous ne voulez pas que la fenêtre DOS s’ouvre, vous devez appeler votre programme par l’interpréteur pythonw (situé dans le même répertoire que l’interpréteur python). En général, il suffit de changer l’extension de votre programme de .py en .pyw. Si la fenêtre DOS s’ouvre encore, il vous faut alors associer manuellement les fichiers .pyw à pythonw. création de code dynamique ========================== .. code-block:: python exec('if LettreCrypte in' + liste + ':\n print liste\n raw_input()') faire un if en une ligne ======================== on peut faire un if comme cela .. code-block:: python a = 2 if a > 1: b = False else: b = True ou .. code-block:: python a = 2 b = a >= 1 and True or False print ===== .. code-block:: python name = 'David' messages = 3 text = ('Hello %s, you have %i messages' % (name, messages)) print text .. code-block:: python values = {'name': name, 'messages': messages} print ('Hello %(name)s, you have %(messages)i ' 'messages' % values) Gestion des erreurs =================== .. code-block:: python try: a = 1/0 except Exception, e: syslog.syslog('[%s][ERROR][ERROR]: %s ' % (sys.argv[0],e)) #sys.argv[0] récupère le nom du programme Création d’erreur ================= La fonction getPathFile lève une erreur si il ne trouve pas le fichier dans son path .. code-block:: python from optparse import OptionParser import sys import os, os.path def getPathFile(p): """ Test of p is file with pwd and PYTHONPATH """ if os.path.isfile(os.path.join(os.getcwd(),p)): return os.path.join(os.getcwd(),p) for i in sys.path: if os.path.isfile(os.path.join(i,p)): return os.path.join(i,p) raise AttributeError, '%s is not directory' % p Test de la version python ========================= .. code-block:: python #!/usr/bin/python # Nom de fichier : versioncheck.py import sys, warnings if sys.version_info[0] < 3: warnings.warn("Need Python 3.0 for this program to run", RuntimeWarning) else: print('Proceed as normal')