python目录与文件名操作例子
1、操作目录与文件名
#!/usr/bin/envpython #-*-coding:utf-8-*- importos,re importshutil importtime
用listdir搜索
defsearch_OFD_old(my_pattern,diretory):
try:
names=os.listdir(diretory)
exceptos.error:
print"error"
return
fornameinnames:
fullname=os.path.normpath(os.path.join(diretory,name))
ifos.path.isfile(fullname):
result=my_pattern.search(name)
ifresultandname.lower().endswith("txt"):
shutil.copy(fullname,dest_dir)
elifos.path.isdir(fullname):
search_OFD(my_pattern,fullname)
用walk函数搜索
defsearch_OFD(my_pattern,diretory):
forroot,dirs,filesinos.walk(diretory):
forfilenameinfiles:
result=my_pattern.search(filename)
ifresultandfilename.lower().endswith("txt"):
fullname=os.path.join(root,filename)
shutil.copy(fullname,dest_dir)
目录不存在,则创建:
ifnotos.path.isdir(dest_dir): os.makedirs(dest_dir)
匹配名称
importre
pattern=re.compile("1ABC")
pattern.search(var)热门推荐
10 生日祝福语朋友简短的话
11 宝贝的人生祝福语简短
12 新年有哪些祝福语简短
13 简短又洋气祝福语大全
14 简短祝福语除夕快乐英文
15 生日贺卡祝福语毕业简短
16 战友聚会祝福语幽默简短
17 最新中秋祝福语简短英语
18 夏日升学宴祝福语简短