perl?(1987) |
python?(1991) |
基础 |
模块导入 |
use?strict; |
import?os,re,sys |
版本查看 ? |
$?perl?-v |
$?python -V |
执行脚本 ? |
$?perl foo.pl |
$?python foo.py |
交互模式 ? |
$?perl -de 0 |
$?python |
执行语句 |
$?perl -e 'print("hin")' |
$?python -c "print('hi')" |
语句分隔 ? |
; |
n (newline) or?; |
语句块 ? |
{} |
Indent |
注释 ? |
# comment |
# comment |
多行注释 ? |
=for comment line another line =cut |
use triple quote string literal: '''comment line another line''' |
变量和操作符 |
赋值? |
$v = 1; |
v?= 1 |
赋值 ? |
($x,?$y,?$z) = (1,2,3); # 3 is discarded: ($x,?$y) = (1,34)">#?$z set to undef:x,?yz?= 1,3 # raises ValueError: y?交换 ? |
= y,x |
操作符 |
+= -= *=?none?/= %=?**= .= x= &&=?||= ^= <<= >>= &= |= ^= |
# do not return values: += -= *= /=?//= %=?**= += *= &=?|= ^= <<= >>= &= |= ^= |
自增 ? |
my?$x?= 1; my?$y?= ++$x; my?$z?=?--$y; |
none |
局部变量 ? |
my?$v; my?(@a%d); my?= 1; my?($y$z) = (2,3); |
# in function body:=?Nonead?= [],{} x?= 1 = 2,3 |
全局变量 |
our?$g1$g2) = (7,8); sub?swap_globals?{ ??($g1,?$g2) = ($g2,?$g1); } |
g1g2?= 7,8 def?swap_globals(): ??global?g1,g2 ??g1,g2 = g2,g1 |
常量 ? |
use?constant?PI?=> 3.14; |
# uppercase identifiers # constant by conventionPI?= 3.14 |
空 ? |
undef |
None |
空测试 ? |
!?defined?$v |
v ==?None v?is?None |
访问未定义变量 ? |
error under?use strict;?otherwise?undef |
raises?NameError |
真假 ? |
1?"" |
True False |
假 ? |
undef?0?0.0?"" "0"?() |
False?None?0?0.0?''?[] {} |
逻辑运算 ? |
&&?||?! lower precedence: and or xor not |
and or not |
条件 ? |
$x > 0 ??$x : -$x |
x?if?x > 0?else?-x |
比较 ? |
numbers only:?== != > < >= <= strings:?eq ne gt lt ge le |
comparison operators are chainable: == != > < >= <= |
数学运算 |
类型转化 ? |
7 +?"12" 73.9 +?".037" "value: "?. 8 |
7 +?int('12') 73.9 +?float('.037') 'value: '?+?str(8) |
算术运算 ? |
+ - * /?none?%?** |
+ - * / // %?** |
取余 ? |
int?( 13 / 5 ) none |
13 // 5 q,r =?divmod(13,5) |
浮点除法 ? |
13 / 5 |
float(13) / 5 # Python 3: 13 / 5 |
数学函数 ? |
use?Math::Trig?qw( ??tan asin acos atan);
sqrt exp log sin cos?tan asin acos atan?atan2 |
from?math?import?sqrt,exp,log, sin,cos,tan,asin,acos,atan,atan2 |
四舍五入 ? |
# cpan -i Number::Format use?Number::Format?'round'; use?POSIX?qw(ceil floor);
int($x) round($x,0) ceil($x) floor($x) abs($x) |
import?math
int(x) int(round(x)) math.ceil(x) math.floor(x) abs(x) |
最大最小 ? |
use?List::Util?qw(min max);
min(1,3); max(1,3); @a?= (1,3); min(@a); max(@a); |
min(1,3) maxmin([1,3]) max除0 ? |
error |
raises?ZeroDivisionError |
大整数 ? |
converted to float; use?Math::BigInt?to create arbitrary length integers |
becomes arbitrary length integer of type?long |
大浮点数 ? |
inf |
raises?OverflowError |
随机数 |
int(rand() * 100) rand() none |
import?random
random.randint(0,99) random.random() random.gauss(0,1) |
随机数 |
srand?17;
my?$sd?=?srand; srand($sd); |
import?random
random.seed(17) sd?= random.getstate() random.setstate(sd) |
位操作 ? |
<< >> & | ^ ~ |
<< >> & | ^ ~ |
其他进制 |
0b101010 052 0x2a |
0b101010 052 0x2a |
字符串操作 |
字符串 ? |
"don't say "no"" 'don't say "no"' |
'don't say "no"' "don't say "no"" "don't "?'say "no"' '''don't say "no"''' """don't say "no"""" |
多行字符串 ? |
yes |
triple quote literals only |
转义 ? |
double quoted: a b cx?e f n r t xhh?x{hhhh} ooo???Perl 5.14:?o{ooo}
single quoted: ' |
single and double quoted: newline? ' " a b f n r t v ooo?xhh
Python 3: uhhhh?Uhhhhhhhh |
变量替换 ? |
my?$count?= 3; my?$item?=?"ball"; print?"$count?${item}sn"; |
count?= 3 item?=?'ball' print(‘%s %s’ % (count,item)) |
sprintf ? |
my?$fmt?=?"lorem %s %d %f"; sprintf($fmt,?"ipsum"'lorem %s %d %f'?% ('ipsum'fmt?=?'lorem {0} {1} {2}' fmt.format('ipsum'here document ? |
$word =?"amet"; $s =?<<EOF; lorem ipsum dolor sit?$word EOF |
‘’’ ‘’’ |
字符串连接 ? |
my?$s?=?"Hello,"; my?$s2?=?$s .?"World!"s?=?'Hello,'s2?= s +?'World!'
juxtaposition can be used to concatenate literals: |
字符串复制 ? |
my?$hbar?=?"-"?x?80; |
hbar?=?'-'?* 80 |
字符串分隔 |
split(/s+/"do re mi fa") splitsplit(/(s+)/); split(//"abcd") |
'do re mi fa'.split() 'do re mi fa'.split(None'(s+)''do re mi fa') list('abcd') |
字符串连接 ? |
join(" "qw(do re mi fa)) |
' '.join(['do''re''mi''fa']) |
字符串大小写 |
uc("lorem") lc("LOREM") ucfirst) |
'lorem'.upper() 'LOREM'.lower() 'lorem'.capitalize() |
字符串strip ? |
# cpan -i Text::TrimText::Trim;
trim?" lorem " ltrim?" lorem" rtrim?"lorem " |
' lorem '.strip() ' lorem'.lstrip() 'lorem '.rstrip() |
字符串格式化 ? |
sprintf("%-10s""lorem") sprintf("%10s".ljust(10) 'lorem'.rjust(10) |
字符串长度 ? |
length) |
len('lorem') |
字符串index? ? |
index("lorem ipsum""ipsum") rindex("do re re""re") return?-1?if not found |
'do re re'.index('re') 'do re re'.rindex('re') raise?ValueError?if not found |
子字符串 ? |
substr'lorem ipsum'[6:11] |
访问字符串中字母 |
can't use index notation with strings: substr'lorem ipsum'[6] |
字母数字转化 ? |
chr(65) ord("A") |
chr(65) ord('A') |
正则表达式 |
字符串或 |
/lorem|ipsum/ qr(/etc/hosts) |
re.compile('lorem|ipsum') none |
特殊字符 |
char class abbrevs: . d D h H s S v V w W
anchors:?^?$?A b B z Z |
char class abbrevs: . d D s S w W
anchors:?^?$?A b B Z |
正则表达式匹配 ? |
if?($s =~?/1999/) { ??print?"party!n"; } |
if?re.search('1999'print('party!') |
忽略大小写 |
"Lorem"?=~?/lorem/i |
re.search('lorem''Lorem'选项 ? |
i m s p x |
re.I re.M re.S re.X |
替换 ? |
my?$=?"do re mi mi mi"; $s =~?s/mi/ma/g=?'do re mi mi mi'= re.compile('mi').sub('ma'group? ? |
$rx =?qr/(d{4})-(d{2})-(d{2})/; "2010-06-03"?=~?$rx; ($yr,?$mo,?$dy) = ($1,?$2,?$3); |
rx?=?'(d{4})-(d{2})-(d{2})'m?= re.search(rx,?'2010-06-03') yrmody?= m.groups() |
findall ? |
my?=?"dolor sit amet"; @a?=?$s =~?m/w+/g=?'dolor sit amet'a?= re.findall('w+'匹配引用 |
"do do"?=~?/(w+) 1/
my?=?"do re"; $s =~?s/(w+) (w+)/$2?$1/; |
none
= re.compile('(w+) (w+)') rx.sub(r'2 1''do re' 日期时间 |
日期时间类型 ? |
Time::Piece?if?use Time::Piece?in effect,otherwise tm array |
datetime.datetime |
当前日期时间 |
use?Time::Piece;
my?t?=?localtime(time); my?utc?=?gmtime); |
import?datetime
= datetime.datetime.now() = datetime.datetime.utcnow() |
与epoch转化 |
use?Time::Localepoch?= timelocal($t); my?t2?(1304442000); |
from?datetime?import?datetime?as?dt
=?int(t.strftime("%s")) = dt.fromtimestamp(1304442000) |
当前epoch |
$epoch =?time)) |
strftime |
use?;
$t =?localtime); $fmt =?"%Y-%m-%d %H:%M:%S"; print?$t->strftime($fmt); |
t.strftime('%Y-%m-%d %H:%M:%S') |
默认格式 |
Tue Aug 23?19:35:19?2011 |
2011-08-23?19:35:59.411135 |
字符串转为时间strptime |
use?;
$s =?"2011-05-03?10:00:00"; $fmt =?"%Y-%m-%d %H:%M:%S"; $t = Time::Piece->strptime($s,$fmt); |
from?=?'2011-05-03?10:00:00'=?'%Y-%m-%d %H:%M:%S'= datetime.strptime(s,fmt) |
解析日期 |
# cpan -i Date::ParseDate::Parse;
$epoch = str2time("July 7,1999"); |
# pip install python-dateutil import?dateutil.parser
=?'July 7,1999'= dateutil.parser.parse(s) |
时间差 |
Time::Seconds?object if?use Time::Piece?in effect; not meaningful to subtract tm arrays |
datetime.timedelta?object |
时间运算 |
use?Time::Seconds;
$now =?localtime); $now += 10 * ONE_MINUTE() + 3; |
import?delta?= datetime.timedelta( ??minutes=10, ??seconds=3) = datetime.datetime.now() + delta |
时区 |
Time::Piece?has local timezone if created withlocaltime?and UTC timezone if created with?gmtime;?tm arrays have no timezone or offset info |
a?datetime?object has no timezone information unless a?tzinfo?object is provided when it is created |
timezone name; offset from UTC;?是否夏令时 |
# cpan -i DateTimeDateTimeDateTime::TimeZone;
$dt = DateTime->now(); $tz = DateTime::TimeZone->new( ??name=>"local");
$tz->name; $tz->offset_for_datetime($dt) / ??3600; $tz->is_dst_for_datetime($dt); |
import?time
tm?= time.localtime() ?? time.tzname[tm.tm_isdst] (time.timezone / -3600) + tm.tm_isdst tm.tm_isdst |
microseconds |
use?Time::HiRes?qw(gettimeofday);
($sec,?$usec) = gettimeofday; |
t.microsecond |
sleep |
a float argument will be truncated to an integer: sleep?1; |
import?time
time.sleep(0.5) |
timeout |
eval?{ ??$SIG{ALRM}=?sub?{die?"timeout!";}; ??alarm?5; ??sleep?10; }; alarm?0; |
import?signal,time
class?Timeout(Exception):?pass
def?timeout_handler(signo,fm): ??raise?Timeout()
signal.signal(signal.SIGALRM, ??timeout_handler)
try: ??signal.alarm(5) ??time.sleep(10) except?Timeout: ??pass signal.alarm(0) |
数组 |
定义 ? |
@a?= [1,4] |
quote words ? |
@a?=?qw(do re mi); |
none |
长度 ? |
$#a?+ 1?or scalar(a) |
空测试 ? |
!@a |
not?a |
使用 ? |
$a[0] |
a[0] |
更新 ? |
$a[0] =?"lorem"; |
a[0] =?'lorem' |
越界访问 |
@a?= (); evaluates as?undef: $a[10]; increases array size to 11:[10] =?"lorem"= [] raises?IndexError: a[10] raises?IndexError: a[10] =?'lorem' |
index |
use?List::Util?'first';
@a?(x y z w); $i = first {$a[$_]?eq?"y"} (0..$#a); |
= ['x''y''z''w'] i?= a.index('y') |
子数组 ? |
select 3rd and 4th elements: @a[2..3] spliceselect 3rd and 4th elements: a[2:4] none |
子数组 ? |
@a[1..$#a] |
a[1:] |
添加删除 ? |
@a?= (6,7,8); push?@apop?@a= [6,8] a.append(9) a.pop() |
插入删除 ? |
@a?unshift?@ashift?@a数组连接 |
@a?@a2?= (@apush?@aa2?= a + [4,6] a.extend([4,6]) |
初始化 |
@a?= (undef)?x?10; |
a = [None] * 10 a = [None for?i?in range(0,10)] |
浅拷贝深拷贝 |
use?Storable?'dclone'
my?@a?my?$a2?= @a; my?@a3?=?@a; my?@a4?= @{dclone(@a)}; |
import?copy
= a a3?=?list(a) a4?= copy.deepcopy(a) |
数组作为函数参数 |
each element passed as separate argument; use reference to pass array as single argument |
parameter contains address copy |
遍历 ? |
for?$i?print?"$in"?} |
for?i?in?[1,3]: ??print(i) |
遍历 |
none; use range iteration from?0?to?$#a?and use index to look up value in the loop body |
= ['do'] for?i,s?in?enumerate(a): ??print('%s at index %d'?% (s,i)) |
range |
for?(1..1_000_000) { ??code } |
range?replaces?xrange?in Python 3: for?i?in xrangecode |
range数组 |
@a?= 1..10; |
=?rangePython 3:(range翻转 |
@a?reverse?@a=?reverse?@a排序 |
@a?(b A a B); sort?@a=?sort?@a; sort?{?lc($a)?cmp?lc($b) }?@a= ['b''A''a''B'] sorted(a) a.sort() a.sort(key=str.lower) |
去重复 |
use?List::MoreUtils?'uniq';
my?@a?my?@a2?= uniq?@a(set(a)) (a)) |
是否存在于数组 ? |
7 ~~?@a |
7?in?a |
集合交集 ? |
{1,2} & {2,4} |
集合或 ? |
集合运算 |
map ? |
map?{?$_ *?$_ } (1,3) |
map(lambda?x: x * x,[1,3]) # or use list comprehension: [x*x for x in [1,3]] |
filter ? |
grep?{?$_ > 1 } (1,3) |
filterx: x > 1,17)"> [x for x in [1,3] if x > 1] |
reduce ? |
use?List::Util?'reduce';
reduce {?$x +?$y } 0,(1,3) |
# import needed in Python 3 only from?functools?import reduce
reducex,y: x+y,3],0) |
All/any ? |
# cpan -i List::MoreUtilsList::MoreUtils?qw(all any);
all {?$_ % 2 == 0 } (1,4) any {?$_ % 2 == 0 } (1,4) |
all(i%2 == 0?for?anyshuffle and sample |
use?List::Util?'shuffle'@a); none |
from?random?import?shuffle,sample
zip ? |
List::MoreUtils?'zip';
@nums?@lets?(a b c); # flat array of 6 elements: @a?= zip?@nums@lets; |
# array of 3 pairs:= zip([1,['a''b''c']) |
字典对象 |
定义 ? |
%d?= (?t?=> 1,?f?=> 0 ); |
= {?'t':1,?'f':0 } |
size ? |
scalar(keys?%d(d) |
lookup ? |
$d{"t"} |
d['t'] |
out-of-bounds behavior ? |
%d? $d{"lorem"}; adds key/value pair:} =?"ipsum"= {} raises?KeyError: d['lorem'] adds key/value pair:] =?'ipsum' |
is key present ? |
exists?$d{"y"} |
'y'?in?d |
delete entry |
%d?= ( 1 =>?"t""f"?); delete?$d{1}; |
= {1:?TrueFalse} del?d[1] |
from array of pairs,from even length array |
@a?"a""b""c"); %d?= [[1,'a'],[2,'b''c']] =?dict(a)
(zip(a[::2],a[1::2])) |
merge |
%d1?= (a=>1,?b=>2); %d2?= (b=>3,?c=>4); %d1?= (%d1%d2d1?= {'a':2} d2?= {'b':3,17)">:4} d1.update(d2) #?{'a': 1,'c': 4,'b': 3} |
invert |
%to_num?= (tf=>0); %to_let?=?reverse?%to_numto_num?= {'t':1,17)">:0} # dict comprehensions added in 2.7:to_let?= {v:k?for?k,v ??in?to_num.items()} |
iteration ? |
while?( ($k,?$v) =?each?%d?) { ??code } |
for?in?d.iteritems(): ??coded.items(): ??code |
keys and values as arrays |
keys?%d values?%d |
d.keys() d.values()
Python 3: list(d.keys()) list(d.values()) |
default value,computed value |
my?%counts; $counts{'foo'} += 1
define a tied hash for computed values and defaults other than zero or empty string |
from?collections?import?defaultdict
counts?= defaultdict(lambda: 0) counts['foo'] += 1
class?Factorial(dict): ??def?__missing__(selfif?k > 1: ??????return?k *?self[k-1] ????else: ??????return?1
factorial?= Factorial() |
函数 |
函数申明 ? |
sub?add?{?$_[0] +?$_[1] }
sub?{ ??my?$a$b) =?@_; ??$a +?$b; } |
def?add(a,b): ??return?a+b |
函数调用 |
add(1,2);
parens are optional: add 1,2; |
参数丢失 ? |
set to?undef |
raises?TypeError |
参数默认值 ? |
sub?my_log?{ ??my?=?shift; ??my?$base?=?shift?// 10;
??log$x)/log$base); }
my_log(42); my_log(42,?exp(1)); |
import?math
def?my_log(x,base=10): ??return?math.log(x)/math.log(base)
my_log(42) my_log(42,math.e) |
变长参数 |
sub?foo?{ ??if?(?@_?>= 1 ) { ????print?"first:?$_[0]n"; ??} ??if?>= 2 ) { ????print?"last:?$_[-1]n"; ??} } |
def?foo(*a): ??if len(a) >= 1: ????print('first: '?(a[0])) ??if len(a) >= 2: ????print('last: '?(a[-1])) |
命名参数 ? |
none |
def?fequaleps?= opts.get('eps')?or?0.01 ??return abs(x - y) < eps
fequal(1.0,1.001) fequal(1.0,1.001,eps=0.1**10) |
pass number or string by reference ? |
sub?{ ??$_[0] += 1; ??$_[1] .=?"ly"; }
my?$n?= 7; my?=?"hard"; foo($n,?$s); |
not possible |
pass array or dictionary by reference ? |
sub?[0][2] = 5; ??$_[1]{"f"} = -1; }
my?@a?my?%d?= ("t""f"?=> 0); foo(@a%d); |
def?'f'] = -1
= {'t':0} foo(a,d) |
return value ? |
return?arg or last expression evaluated |
return?arg or?None |
multiple return values ? |
sub?first_and_second?{ ??return?($_[0],?$_[1]); } @a?@afirst_and_second(a): ??return?a[0],a[1]
= first_and_second([1,3]) |
lambda declaration ? |
$sqr =?sub?[0] *?$_[0] } |
body must be an expression:sqr?=?lambda?x: x * x |
lambda invocation |
$sqr->(2) |
sqr(2) |
function reference |
my?$func?= &;add; |
func?= add |
function with private state |
use?feature?state;
sub?counter?{ ??state?$i = 0; ??++$i; }
print?counter() .?"n"# state not private: def?counter(): ??counter.i += 1 ??return?counter.i
counter.i = 0 print(counter()) |
closure |
sub?make_counter?= 0; ??return?sub?{ ++$i }; } my?$nays?= make_counter; print?$nays->() .?"n"make_counter(): ??i = 0 ??def?(): ????nonlocal i ????i += 1 ????return?i ??return?counter
nays = make_counter() |
generator |
none |
def?(): ??i = 0 ??while True: ????i += 1 ????yield?i
nays?= make_counter() print(nays.next()) |
decorator |
def logcall(f): ??def wrapper(*a,**opts): ????print('calling ' + f.__name__) ????f(*a,**opts) ????print('called ' + f.__name__) ??return wrapper
@logcall def square(x): ??return x * x
square(5) |
流程控制 |
if ? |
if?( 0 ==?$n ) { ??print?"no hitsn" }?elsif?( 1 ==?$n ) { ??print?"one hitn" }?else?{ ??print?"$n hitsn" } |
if?0 == n: ??print('no hits') elif?1 == n: ??print('one hit') else: ??print(str(n) +?' hits') |
switch |
use?feature?'switch';
given ($n) { ??when (0) {?print?"no hitsn"; } ??when (1) {?print?"one hitn"; } ??default {?print?"$n hitsn"; } } |
none |
while ? |
while?(?$i < 100 ) {?$i++ } |
while?i < 100: ??i += 1 |
c-style for ? |
for?(?$i=0;?$i <= 10;?$i++ ) { ??print?"$in"; } |
none |
Foreach |
@a = (1..5); foreach? (@a) { ?? print "$_n"; } ? @a = (1..5); for (@a) { ?? print "$_n" } |
a = ['do','re','mi','fa'] for i,s in enumerate(a): ? print('%s at index %d' % (s,i)) ? for i in [1,3]: ? print(i) |
break,continue,redo ? |
last next redo |
break continue?none |
control structure keywords |
do else elsif for foreach goto if unless until while |
elif else for if while |
what do does |
executes following block and returns value of last statement executed |
raises?NameError?unless a value was assigned to it |
statement modifiers ? |
print?"positiven"?if?$i > 0; print?"nonzeron"?unless?$i == 0; |
none |
raise exception ? |
die?"bad arg"; |
raise?Exception('bad arg') |
catch exception ? |
eval?{ risky }; if?($@) { ??print?"risky failed:?$@n"; } |
try: ??risky() except('risky failed') |
global variable for last exception |
$EVAL_ERROR:?$@ $OS_ERROR:?$! $CHILD_ERROR:?$? |
last exception:?sys.exc_info()[1] |
define exception |
none |
class?Bam): ??def?__init__): ????super(Bam,?self).__init__('bam!') |
catch exception by type |
none |
try: ??raise?Bam() except?Bam?as?e: ??print(e) |
finally/ensure ? |
none |
acquire_resource() try: ??risky() finally: ??release_resource() |
start thread ? |
use?threads;
$func =?sub?{?sleep?10 }; $thr = threads->new($func); |
class?sleep10(threading.Thread): ??def?run): ????time.sleep(10)
thr?= sleep10() thr.start() |
wait on thread ? |
$thr->jointhr.join() |
文件和输出 |
print to standard output ? |
print?"Hello,World!n"; |
print('Hello,World!') |
read from standard input |
$line = <STDIN>; |
line?= sys.stdin.readline() |
standard file handles ? |
STDIN STDOUT STDERR |
sys.stdin sys.stdout sys.stderr |
open file ? |
open?my?$f"/etc/hosts";?or open?FILE,63)">f?=?open('/etc/hosts') |
open file for writing ? |
open?my?">/tmp/perl_test"('/tmp/test') |
open file for append |
with open)?as?f: ??f.write('lorem ipsumn') |
close file ? |
close?$f;?or close?FILE; |
f.close() |
read line ? |
$line = <$f>;?or $line = <FILE>; |
f.readline() |
iterate over file by line ? |
while?($line = <$f>) { |
for?line?in?f: |
chomp ? |
chomp?$line; |
= line.rstrip('rn') |
read entire file into array or string |
@a?= <$f>; $s = do { local?$/; <$f> }; |
= f.readlines() = f.read() |
write to file ? |
print?$f?"lorem ipsum"f.write('lorem ipsum') |
flush file handle ? |
use?IO::Handle;
$f->flush(); |
f.flush() |
file test,regular file test ? |
If (-e?"/etc/hosts") {print exist;} -f?"/etc/hosts" |
os.path.exists('/etc/hosts') os.path.isfile('/etc/hosts') |
copy file,remove file,rename file |
use?File::Copy;
copy("/tmp/foo""/tmp/bar"); unlink?"/tmp/foo"; move("/tmp/bar""/tmp/foo"); unlink(); |
import?shutil
shutil.copy('/tmp/foo''/tmp/bar') os.remove('/tmp/foo') shutil.move('/tmp/bar''/tmp/foo') os.remove() |
set file permissions |
chmod?0755,17)">os.chmod('/tmp/foo'temporary file |
use?File::Temp;
$f = File::Temp->new(); print?$f?"lorem ipsumn"; close?$f;
print?"tmp file: "$f->filename .?"n"tempfile
= tempfile.NamedTemporaryFile( ??prefix='foo') f.write('lorem ipsumn') f.close()
print("tmp file: %s"?% f.name) |
in memory file |
my?$s); open">"); print?$f?"lorem ipsumn"; ; |
from?StringIO?import?StringIO
= StringIO() f.write('lorem ipsumn'= f.getvalue()
Python 3 moved?StringIO?to the?io?module |
目录操作 |
build pathname |
use?File::Spec;
File::Spec->catfile("/etc""hosts") |
os.path.join('/etc''hosts') |
dirname and basename |
use?File::Basename;
print?dirname("/etc/hosts"); print?basename("/etc/hosts"); |
os.path.dirname('/etc/hosts') os.path.basename('/etc/hosts') |
absolute pathname |
use?Cwd;
Cwd::abs_path(".."os.path.abspath('..') |
iterate over directory by file |
use?;
while?(?</etc/*>?) { ???print?basename($_) .?"n"; } |
for?filename?in?os.listdir('/etc'): ??print(filename) |
make directory |
use?File::Path?'make_path';
make_path?"/tmp/foo/bar"dirname?=?'/tmp/foo/bar' if not?os.path.isdir(dirname): ??os.makedirs(dirname) |
recursive copy |
# cpan -i File::Copy::RecursiveFile::Copy::Recursive?'dircopy';
dircopy?"/tmp/foodir""/tmp/bardir"shutil
shutil.copytree('/tmp/foodir''/tmp/bardir') |
remove empty directory |
rmdir?"/tmp/foodir"os.rmdir('/tmp/foodir') |
remove directory and contents |
use?File::Path?'remove_tree';
remove_tree?"/tmp/foodir"shutil
shutil.rmtree('/tmp/foodir') |
directory test ? |
-d?"/tmp" |
os.path.isdir('/tmp' 命令行操作 |
command line args,script name ? |
scalar(@ARGV) $ARGV[0]?$ARGV[1]?etc $0 |
len(sys.argv)-1 sys.argv[1] sys.argv[2]?etc sys.argv[0] |
getopt |
use?Getopt::Long$src$help);
sub?usage?{ ??print?"usage:?$0?--f SRCn"; ??exit?-1; }
GetOptions("file=s"?=>?$src, ??"help"?=>?$help);
usage?if?$help; |
import?argparse
parser?= argparse.ArgumentParser() parser.add_argument('--file''-f''file')
args?= parser.parse_args() src?= args.file |
get and set environment variable ? |
$ENV{"HOME"}
$ENV{"PATH") =?"/bin"os.getenv('HOME')
os.environ['PATH'] =?'/bin' |
exit ? |
exit?0; |
sys.exit(0) |
set signal handller ? |
$SIG{INT} =?sub?{ ??die?"exiting…n"; }; |
import?signal
def?handlerprint('exiting…') ??exit?-1 signal.signal(signal.SIGINT,handler) |
executable test |
-x?"/bin/ls" |
os.access('/bin/ls'external command ? |
system("ls -l /tmp") == 0?or ??die?"ls failed"; |
if?os.system('ls -l /tmp'): ??raise?Exception('ls failed') |
escaped external command ? |
$path = <>; chomp($path); system("ls""-l"orsubprocess
cmd?= ['ls''-l''/tmp'] if?subprocess.call(cmd): ??raise?Exception) |
backticks ? |
my?$files?=?`ls -l /tmp` my?=?qx(ls); |
import?files?= subprocess.check_output(cmd) |
__________________________________________ |