易截截图软件、单文件、免安装、纯绿色、仅160KB

Python 3 教程二:文件,目录和路径

1 遍历文件夹和文件
import  os
import  os.path
#  os,os.path里包含大多数文件访问的函数,所以要先引入它们.
#  请按照你的实际情况修改这个路径
rootdir  =   " d:/download "
for  parent, dirnames, filenames  in  os.walk(rootdir):
     # case 1:
     for  dirname  in  dirnames:
         print  ( " parent is: "   +  parent)
         print  ( " dirname is: "   +  dirname)
     # case 2
     for  filename  in  filenames:
         print  ( " parent is: "   +  parent)
         print  ( " filename with full path : "   +  os.path.join(parent, filename))
''' 知识点:
    * os.walk返回一个三元组.其中dirnames是所有文件夹名字(不包含路径),filenames是所有文件的名字(不包含路径).parent表示父目录.
    * case1 演示了如何遍历所有目录.
    * case2 演示了如何遍历所有文件.
    * os.path.join(dirname,filename) : 将形如"/a/b/c"和"d.java"变成/a/b/c/d.java".
'''
2 分割路径和文件名
import  os.path
# 常用函数有三种:分隔路径,找出文件名.找出盘符(windows系统),找出文件的扩展名.
# 根据你机器的实际情况修改下面参数.
spath = " D:/download/repository.7z "
#  case 1:
p,f = os.path.split(spath);
print ( " dir is: " + p)
print ( " file is: " + f)
#  case 2:
drv,left = os.path.splitdrive(spath);
print ( " driver is: " + drv)
print ( " left is: " + left)
#  case 3:
f,ext = os.path.splitext(spath);
print ( " f is: " + f)
print ( " ext is: " + ext)
'''
&nb


相关文档:

用python, c#, java写的文件拷贝

     前一段时间试着用这三种语言简单的写了关于文件拷贝的程序,发现c#和python的api惊人的相似,对于文件的操作这两种语言非常的方便。都没有加异常的处理
C#源代码:
        public static void CopyFile(string source, string destination)
  & ......

[转]Python: python编码问题

这是一个我们在处理中文时, 经常遇到的问题.
python里面基本上要考虑三种编码格式
1 源文件编码
在文件头部使用coding声明。告诉python解释器该代码文件所使用的字符集。
#/usr/bin/python
#coding: utf8
2 内部编码
代码文件中的字符串,经过decode以后,被转换为统一的unicode格式的内部数据,类似于u'*'。unic ......

python decorator

1.常用方法,不带参数
def decator(func):
    def inner_func(*args):
        args = (i * 2 for i in args)
        return func(*args)
    return inner_func
   
@decator
def add(a, ......

Python and RRD

RRD是Round Robin Database的意思,RRDTool是用来管理RRD的一个工具。RRDTool的主页在这里,Wikipedia的页面在这里。RRD其实就是一个时序数据库,使用一个固定大小的环型buffer,适用于存储一些统计性的信息,如CPU负载呀,气温变化呀。我为什么要说这个东西呢,因为XenServer里的性能统计是用的RRD,你可以访问诸如http:// ......

Python 有权重的随机选择, Weighted Random Choice

import random def windex(lst):
    '''an attempt to make a random.choose() function that makes weighted choices
    accepts a list of tuples with the item and probability as a pair'''
    wtotal = sum([x[1] for x in lst])
   ......
© 2009 ej38.com All Rights Reserved. 关于E健网联系我们 | 站点地图 | 赣ICP备09004571号