Хабр Курсы для всех
РЕКЛАМА
Практикум, Хекслет, SkyPro, авторские курсы — собрали всех и попросили скидки. Осталось выбрать!
Только вот подобный способ будет работать только при наличии bash или подобного ему интерпретатора (ну или потребует прописания алиасов).
On Unix, with shell=False (default): In this case, the Popen class uses
os.execvp() to execute the child program. args should normally be a sequence.
A string will be treated as a sequence with the string as the only item
(the program to execute).
#!/usr/bin/perl
while (<>) {
print;
}
#! /usr/bin/python
import sys
while True:
l = sys.stdin.readline()
print l,
if not l:
break
import sys
# пишем генератор
def lines(file):
while 1:
line=file.readline()
if line:
yield line
else:
break
# пользуемся
for line in lines(sys.stdin):
print ">", line
import sys
for line in sys.stdin:
print l
# а почему не так?def __del__(self):
"""Destructor. Calls close()."""
try:
self.close()
except:
passДо сих пор помню этот ужас, который приходилось писать на голом Си — считывание файла построчно в переменную и перезапись его по другому адресу…И дальше идет пример с вызовом Popen, который на голом Си делается точно так же в одну строчку, только с system вместо Popen. Зачем вы так?
from __future__ import with_statement
with open("test.in", "rb") as infile:
with open("test.in.inv", "wb") as outfile:
outfile.write(''.join([chr(~ord(x)&0xFF) for x in infile.read()]))
using System;
using System.IO;
class Sample
{
static void Main() {
using(FileStream infile = File.OpenRead(@"test.in")) {
using(FileStream outfile = File.OpenWrite(@"test.in.inv")) {
int b;
while (-1 != (b = infile.ReadByte())) {
outfile.WriteByte((byte)~b);
}
}
}
}
}
#!/usr/bin/python
# -*- coding: utf-8 -*-
import sys
import xmpp
login='server'
pwd='xxxxxxx'
cnx=xmpp.Client('myhost.ru')
cnx.connect( server=('talk.google.com',5223) )
cnx.auth(login,pwd, 'botty')
mess=«Сообщение от сервера myhost.ru\n»+sys.argv[1]
cnx.send( xmpp.Message( «admin@myhost.ru» ,mess ) )
Скрипты Python против Bash — 2 или Tips & Tricks