易截截图软件、单文件、免安装、纯绿色、仅160KB
热门标签: c c# c++ asp asp.net linux php jsp java vb Python Ruby mysql sql access Sqlite sqlserver delphi javascript Oracle ajax wap mssql html css flash flex dreamweaver xml
 最新文章 : Python

python 执行系统命令比较

转载自:http://purpen.javaeye.com/blog/98095
python 执行系统命令比较
关键字: python os system 系统命令
在此比较一下两种方法执行系统命令的方法,以方便于日后运用:(
1. os.system()
system(command) -> exit_status  
      
Execute the command (a string) in a subshell.  
  
  
# 仅仅在一个子终端运行系统命令,而不能获取命令执行后的返回信息..  
 
>>> os.system('ls')  #如果再命令行下执行,结果直接打印出来
04101419778.CHM  bash     document   media     py-django  video  
11.wmv           books    downloads  Pictures  python  
all-20061022     Desktop  Examples   project  ......

python性能增强工具shedskin 0.3版发布

http://code.google.com/p/shedskin/ Shed Skin 0.3 - support for 3 new standard library modules (now about 20 in total): - itertools (jeremie roquet) - heapq (jeremie roquet) - csv (converted using shedskin) - 4 new example programs (now 44 in total!): - maximum weighted matching algorithm (joris van rantwijk) - kanoodle: knuth's dancing links (david austin) - bidirectional dijkstra algorithm (from networkx, uses heapq) - barnes-hut force calculation - improved type inference (scalability, bugfixes, major cleanup) - support for 'map', 'filter', 'reduce' and 'next' - support for 'with' statement (jeremie roquet) - support for 'key' argument of 'sorted' (and 'list.sort') - reorganized codebase, distutils setup.py (thomas spura) - optimized list indexing (joris van rantwijk) - optimized addition of 1-length lists and strings - improved forward referencing of variables/functions - avoid GCC warnings after 'shedskin -e' - support for p ......

Python的操作符

1.#:注释符
2.比较运算符(基本和Java类似)
<  <=  >  >=   ==   !=   <>
Python目前支持两种"不等于"比较运算符,!=和<>,分别是C风格和ABC/Pascal风格。目前后者慢慢地被淘汰了,
推荐使用前者。
在Python中,支持3<4<5这样的表达式,既简洁又优美。它实际上是下面表达式的缩写:
3<4 and 4<5
建议:合理使用括号以增加代码的可读性
在很多场合使用括号都是是一个好主意。括号在Python中不是必须存在的,不过为了可读性,使用括号总是值得的。任何维护你代码
的人都会感谢你,在你再次阅读自己的代码时,你也会感谢你自己。
3.逻辑运算符
and or not
4.布尔型
True和False(首字母均为大写) ......

Python的字符串

Python中字符串被定义为引号之间的字符集合。Python支持使用成对的单引号或双引号,三引号包含的字符串。
使用索引操作符([])和切片操作符([:])可以得到子字符串。字符串有其特有的索引规则:第一个字符的索引是0
,最后一个字符的索引是-1。
加号(+)用于字符串连接运算,星号(*)则用于字符串重复。如下例:
pystr = "Python"
pystr[2:5]结果为:tho
'''
Created on 2010-1-12
@author: cuser
'''
s = "abcd"
'''
print(s[0])
print(s[1])
print(s[2])
print(s[3])
print(s[-1])#最后一个字符的索引为-1
'''
print(s[0:1])
print(s[0:2])
print(s[2:3])
print(s[:2])
print(s[2:])
print(s*2)
print(s*3)
结果如下:
a
ab
c
ab
cd
abcdabcd
abcdabcdabcd ......

c盘整理 Python脚本

能整理大部分无用文件
#!/usr/bin/python
#syscleaner.py
import os
import os.path
#delete files and directory recursively
def itedel(dir):
print('in dir :'+dir)
for doc in os.listdir(dir):
try:
if(os.path.isdir(doc)):
itedel(dir+'\\'+doc)
os.rmdir(dir+'\\'+doc)
else:
os.remove(dir+'\\'+doc)
print('removed :'+dir+'\\'+doc)
except:
pass


dir=os.environ['systemdrive']
ext=['tmp','_mp','log','gid','chk','old']
print('in dir :'+dir)
for file in os.listdir(dir):
try:
if(os.path.splitext(file)[1][1:].lower() in ext):
os.remove(dir+'\\'+file)
print('removed :'+dir+'\\'+file)
except:
pass
dir+='\\recycled'
if (os.path.isdir(dir)):
itedel(dir)

dir=os.environ['windir']
if (os.path.isdir(dir)):
print('in dir :'+dir)
for file in os.listdir(dir): ......

c盘整理 Python脚本

能整理大部分无用文件
#!/usr/bin/python
#syscleaner.py
import os
import os.path
#delete files and directory recursively
def itedel(dir):
print('in dir :'+dir)
for doc in os.listdir(dir):
try:
if(os.path.isdir(doc)):
itedel(dir+'\\'+doc)
os.rmdir(dir+'\\'+doc)
else:
os.remove(dir+'\\'+doc)
print('removed :'+dir+'\\'+doc)
except:
pass


dir=os.environ['systemdrive']
ext=['tmp','_mp','log','gid','chk','old']
print('in dir :'+dir)
for file in os.listdir(dir):
try:
if(os.path.splitext(file)[1][1:].lower() in ext):
os.remove(dir+'\\'+file)
print('removed :'+dir+'\\'+file)
except:
pass
dir+='\\recycled'
if (os.path.isdir(dir)):
itedel(dir)

dir=os.environ['windir']
if (os.path.isdir(dir)):
print('in dir :'+dir)
for file in os.listdir(dir): ......

自动解压大量压缩文件 Python 脚本

之前写了一个自动解压压缩文件到压缩文件所在文件夹的脚本
后根据自己需要,写了另外两个。原理一样
都是使用winrar的命令
第一个脚本没考虑周到,只能解压rar文件
改进后可以支持winrar支持的各种文件
把指定文件夹下的文件保存到指定文件夹
#rardir.py
import os
import sys
src=sys.argv[1]
dst=sys.argv[2]
format=['rar','zip','7z','ace','arj','bz2','cab','gz','iso','jar','lzh','tar','uue','z']
os.chdir(sys.argv[1])
for file in os.listdir('.'):
if os.path.isfile(file) and (os.path.splitext(file)[1][1:].lower() in format)==True:
#cmd='winrar x -ibck "'+file+'" "'+dst+'\\'+os.path.splitext(file)[0]+'\\"'
cmd='winrar x -ibck "'+file+'" "'+dst+'\\"'
os.system(cmd)
os.remove(file)
print('done '+file)
第一个版本的改进
#rardecmp.py
#decompress with winrar
#arguments :filename directory opt
# opt='mkdir' to create directory with the correspond filename
# opt='direct' to decompress rar files in current directory
# opt='mk& ......
总记录数:695; 总页数:116; 每页6 条; 首页 上一页 [56] [57] [58] [59] 60 [61] [62] [63] [64] [65]  下一页 尾页
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号