Console I/O
|
nombre = raw_input('Entrez un nombre > ')
print(nombre)
print "Texte %r %r." % (variable1, variable2)
print "Texte",
print("Texte"),
|
|
chaine2 = chaine1 + "chaine"
chaine2 = "%schaine" % (chaine1)
chaine1 += ";"
chaine1.find('element2')
chaine2 = chaine1.replace(".", "!")
chaine = "chaine"
chaine[0]
chaine[0:2]
chaine[:2]
chaine[2:4]
chaine[2:]
chaine[-1]
chaine[-2]
chaine[-2:]
chaine[:-2]
longueur = len(chaine)
if not chaine1:
|
%s vs %r
%s réalise une convertion avec str(), alors que %r utilise repr().
repr() retourne une syntaxe python valide et qui permet de manière non ambigu de reconstruire l'objet de base.
|
d = datetime.date.today()
str(d)
repr(d)
|
Encodage
|
import sys
print(sys.getdefaultencoding())
import locale
print(locale.getdefaultlocale())
import sys
print(sys.stdout.encoding)
|
|
"é"
u"é"
|
Une autre solution pour spécifier l'encodage du script est de convertir le fichier en UTF-8 avec BOM. Ce qui équivaut à la ligne # coding: utf-8
Exemple
Fichier encodé en ANSI
|
print unicode("é褀", 'cp1252').encode('cp850', 'replace')
|
Fichier encodé en UTF-8 sans BOM
|
print "é"
print u"é"
|
Convertion UTF-8 → cp580
|
file = open('monfichier.txt', 'r')
print file.read().decode('utf-8').encode(sys.stdout.encoding, 'replace')
|
UTF-8 |
→ |
Unicode |
→ |
cp850
|
|
.decode('utf-8') |
|
.encode('cp850') |
|
Le fichier en entrée |
|
Chaîne Python |
|
La console en sortie
|
Openpyxl is a Python library for reading and writing Excel 2010 xlsx/xlsm/xltx/xltm files.
|
import sys
import os, os.path
import openpyxl
import csv
if len(sys.argv) != 2:
exit('Utilisation: {} /path/to/file.xlsx'.format(os.path.basename(sys.argv[0])))
wb = openpyxl.load_workbook(sys.argv[1])
sh = wb.get_active_sheet()
with open('Data.csv', 'w') as f:
csv_writer = csv.writer(f)
for row in sh.rows:
csv_writer.writerow([cell.value for cell in row])
|
|
from ftplib import FTP
ftp = FTP('adresse ftp', 'identifiant', 'mot de passe')
ftp.cwd("dossier")
ftp.mkd("dossier")
fichier = open('chemin vers un fichier','rb')
ftp.storbinary('STOR xxx.pdf', fichier)
fichier.close()
ftp.quit()
|
|
progress = FtpProgess('chemin vers un fichier')
ftp.storbinary('STOR xxx.pdf', fichier, callback=progress.FtpCallback)
class FtpProgess:
def __init__(self, filePath):
self.FileSize = os.path.getsize(filePath)
self.DataSentSum = 0
self.PreviousPercentage = 0
def FtpCallback(self, buffer):
self.DataSentSum += 8192
if not self.PreviousPercentage == self.DataSentSum * 100 / self.FileSize:
self.PreviousPercentage = self.DataSentSum * 100 / self.FileSize
print "%s%%" % self.PreviousPercentage
def FtpCallback(buffer):
print "."
|
Envoyer un email
|
sender = 'Nom <sender@domaine.fr>'
receiver = 'receiver@domaine.fr'
message = """From: sender@domaine.fr
To: receiver@domaine.fr
MIME-Version: 1.0
Content-type: text/html
Subject: Mon sujet
<h1>Titre</h1>
%s
""" % texte
try:
smtpObj = smtplib.SMTP('localhost')
smtpObj.sendmail(sender, receiver, message)
print ("Successfully sent email")
except Exception as e:
print ("Error: unable to send email\n%s" % e)
|
|
sudo pip2 install imapclient
|
|
from imapclient import IMAPClient
import email
HOST = 'imap.gmail.com'
USERNAME = 'compte@gmail.com'
PASSWORD = 'pass'
ssl = True
server = IMAPClient(HOST, use_uid=True, ssl=ssl)
try:
server.login(USERNAME, PASSWORD)
except IMAPClient.Error as e:
exit(e)
select_info = server.select_folder('INBOX')
messages = server.search(['UNSEEN'])
print "%d messages non-lus" % len(messages)
print
print "Messages:"
response = server.fetch(messages, ['RFC822'])
for msgid, data in response.iteritems():
msg = email.message_from_string(data['RFC822'])
print msg['subject']
print msg['from']
print msg.get_payload(0)
server.logout()
|
Tkinter
Bibliothèque permettant de faire des interfaces graphiques en Python.
|
from Tkinter import *
root = Tk()
root.title('Message')
Message(root, text="Text", bg='royalblue', fg='ivory', relief=GROOVE)
.pack(padx=10, pady=10)
root.mainloop()
|
API Windows
Python Win32 Extensions
Gnome Keyring
|
sudo pacman -S python2-gnomekeyring
|
|
import gnomekeyring as GnomeKeyring
for keyring in GnomeKeyring.list_keyring_names_sync():
for id in GnomeKeyring.list_item_ids_sync(keyring):
item = GnomeKeyring.item_get_info_sync(keyring, id)
print("[trousseau: %s] description: %s = mot de passe: %s" %
(keyring, item.get_display_name(), item.get_secret())
else:
if len(GnomeKeyring.list_item_ids_sync(keyring)) == 0:
print '[trousseau: %s] --empty--' % keyring
keyring = GnomeKeyring.get_default_keyring_sync()
GnomeKeyring.item_create_sync(
keyring, GnomeKeyring.ITEM_GENERIC_SECRET, "description de l'entrée",
{ "attribut1" : "valeur", "attribut2" : "valeur" }, "mot de passe", True)
for id in GnomeKeyring.list_item_ids_sync(keyring):
item = GnomeKeyring.item_get_info_sync(keyring, id)
if item.get_display_name() == "GMail Checker":
print("pass: %s" % item.get_secret())
break
else:
print("Entry not found in Gnome Keyring")
try:
matches = GnomeKeyring.find_items_sync(
GnomeKeyring.ITEM_GENERIC_SECRET, {"attribut1" : "valeur"});
for match in matches:
info = GnomeKeyring.item_get_info_sync(keyring, match.item_id)
print("name: %s ; id: %s ; pass: %s; attributs: %s" %
(info.get_display_name(), match.item_id, match.secret, match.attributes))
except GnomeKeyring.NoMatchError:
print "Attribut not found in Gnome Keyring"
|
Script Nautilus
|
import os
for f in os.environ['NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'].splitlines():
|
Script Python PyUNO
DocumentConverter (Windows)
Sous Windows, il est conseillé d'utiliser l'exécutable Python d'OpenOffice car il est déjà configurer pour utiliser UNO.
Le script Python DocumentConverter est disponible ici. Il semble pouvoir fonctionner sur Linux aussi, même si UnoConv semble plus pratique.
|
"C:\Program Files\OpenOffice.org 3\program\soffice.exe" -headless -nofirststartwizard -accept=socket,host=localhost,port=2002;urp;"
"C:\Program Files\OpenOffice.org 3\program\python.exe" DocumentConvert.py document.odt sortie.pdf
|
UnoConv (Linux)
Script python, fonctionnant sous Linux et installable via Synaptic
|
unoconv -f pdf Document.odt
|
Scripts réutilisables
CopyFiles
Équivalent de shutil.copytree, sauf que destination peut déjà exister. L'exclusion des fichiers se fait ici à l'aide d'une expression rationnelle.
|
def CopyFiles(source, destination):
print "CopyFiles " + source + " -> " + destination
if not os.path.exists(destination):
os.mkdir(destination)
for filename in os.listdir(source):
if not re.match(exclusion, filename):
if os.path.isfile(os.path.join(source, filename)):
shutil.copy(os.path.join(source, filename), destination)
else:
CopyFiles(os.path.join(source, filename), os.path.join(destination, filename))
|
Erreurs
TypeError: not all arguments converted during string formatting
Mauvaise utilisation du formatage d'un string.
|
'texte %r." % (variable1 ,variable2)
|
SyntaxError: Non-ASCII character
Depuis Python 2.3 il faut préciser l'encodage au début du script pour disposer de l'ASCII étendu, ISO ou UTF.
invalid syntax with print
print "Hello" SyntaxError: invalid syntax
En python 3.*, la fonction print doit être utilisée avec de parenthèses.
unicodeescape
SyntaxError: (unicode error) 'unicodeescape' codec can't decode bytes in position 2-4: truncated \UXXXXXXXX escape
Une chaîne de caractères contient \U qui est compris comme un Unicode escape de 8 caractères. Pour éviter cela, il suffit de doubler les \ ou bien de préfixer la chaîne avec r : r'ma chaine'.
Exemples
ConverToANSI
Script utilisé par Nautilus pour encoder les fichiers UTF-8 en ASCII.
|
import sys
import os
from Tkinter import *
import codecs
global filesConverted
filesConverted = []
def Convert(filePath):
fichier = open(filePath, "rb")
contenuDuFichier = fichier.read()
contenuDuFichier = contenuDuFichier.decode("utf-8")
fichier.close()
contenuDuFichier = contenuDuFichier.encode("ASCII", 'replace')
fichierTemp = open("tempASCII", "w")
fichierTemp.write(contenuDuFichier)
fichierTemp.close()
os.remove(filePath)
os.rename("tempASCII", filePath)
filesConverted.append(filePath + "\n")
for path in os.environ['NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'].splitlines():
if os.path.isfile(path):
Convert(path)
else:
for root, dirs, files in os.walk(path):
for fileName in files:
Convert(os.path.join(root, fileName))
root = Tk()
root.title('Résultat de la conversion')
Label(root, text="".join(filesConverted)).pack(padx=10, pady=10)
root.mainloop()
|
SendToFTP
Script utilisé par Nautilus pour uploader un fichier vers un serveur FTP.
|
import sys
import os
from ftplib import FTP
from Tkinter import *
global ftp
def SendFile(filePath):
fileNameForFtp = filePath.rsplit('/', 1)[1].
replace(" - ", "_").
replace(" ", "_").replace("#", "Sharp")
fichier = open(filePath, "rb")
ftp.storbinary("STOR " + fileNameForFtp, fichier)
fichier.close()
ftp = FTP("bananeatomic.free.fr", "bananeatomic", "gt27h!i1")
ftp.cwd("files")
for selectedPath in os.environ['NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'].splitlines():
if os.path.isdir(selectedPath):
for filePath in os.listdir(selectedPath):
if os.path.splitext(filePath)[1] == ".pdf":
SendFile(os.path.join(selectedPath, filePath))
else:
SendFile(selectedPath)
ftp.quit()
messageBox = Tk()
messageBox.title("Message")
Label(messageBox, text=os.environ['NAUTILUS_SCRIPT_SELECTED_FILE_PATHS']).
pack(padx=10, pady=10)
messageBox.mainloop()
|
ConvToPdfSendToFTP
Script utilisé par Nautilus pour convertir un fichier odt en pdf et ensuite l'uploader vers un serveur FTP.
|
import sys
import os, os.path
import time
from ftplib import FTP
from Tkinter import *
global ftp
def CreatePDF(odtFilePath):
os.system("unoconv -f pdf \"%s\"" % (odtFilePath))
def SendPDF(odtFilePath):
pathToFile = os.path.dirname(odtFilePath)
fileName = os.path.basename(odtFilePath)
fileNameForFtp = fileName.replace(" - ", "_").
replace(" ", "_").replace("#", "Sharp")
fileNameForFtp = os.path.splitext(fileNameForFtp)[0] + ".pdf"
pdfFilePath = os.path.splitext(odtFilePath)[0] + ".pdf"
fichier = open(pdfFilePath, "rb")
ftp.storbinary("STOR " + fileNameForFtp, fichier)
fichier.close()
ftp = FTP("bananeatomic.free.fr", "bananeatomic", "gt27h!i1")
ftp.cwd("files")
for selectedPath in os.environ['NAUTILUS_SCRIPT_SELECTED_FILE_PATHS'].splitlines():
if os.path.isdir(selectedPath):
for filePath in os.listdir(selectedPath):
if os.path.splitext(filePath)[1] == ".odt":
CreatePDF(os.path.join(selectedPath, filePath))
SendPDF(os.path.join(selectedPath, filePath))
else:
CreatePDF(selectedPath)
SendPDF(selectedPath)
ftp.quit()
messageBox = Tk()
messageBox.title("Message")
Label(messageBox, text=os.environ['NAUTILUS_SCRIPT_SELECTED_FILE_PATHS']).
pack(padx=10, pady=10)
messageBox.mainloop()
|