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代码
import string, os, sys
dir = '/var'
print '----------- no sub dir'
files = os.listdir(dir)
for f in files:
......
8.8. queue — A synchronized queue class¶
queue -- 一个同步队列类
The queue module implements multi-producer, multi-consumer queues. It is especially useful in threaded programming when information must be exchanged safely between multiple threads. The Queue class in this module implemen ......
threading — Higher-level threading interface
This module constructs higher-level threading interfaces on top of the lower level _thread module. See also the queue module.
The dummy_threading module is provided for situations where threading cannot be used because _thread is missing.
......
写了几个有关operaminimod的python小程序
firefox->opm书签转换
import re
def pipeiwangzhi(a):
s=[]
pp= re.compile(r'<DT><A HREF="(.*)" ADD_DATE=(.*>)(.*)</A>')
m=pp.search(a)
s1=[]
  ......