python目錄與文件名操作例子
更新時(shí)間:2016年08月28日 13:05:01 投稿:mdxy-dxy
這篇文章主要介紹了python目錄與文件名操作例子,需要的朋友可以參考下
1、操作目錄與文件名
#!/usr/bin/env python #-*- coding: utf-8 -*- import os,re import shutil import time
用listdir搜索
def search_OFD_old(my_pattern, diretory):
try:
names = os.listdir(diretory)
except os.error:
print "error"
return
for name in names:
fullname = os.path.normpath(os.path.join(diretory, name))
if os.path.isfile(fullname):
result = my_pattern.search(name)
if result and name.lower().endswith("txt"):
shutil.copy(fullname, dest_dir)
elif os.path.isdir(fullname):
search_OFD(my_pattern, fullname)
用walk函數(shù)搜索
def search_OFD(my_pattern, diretory):
for root,dirs,files in os.walk(diretory):
for filename in files:
result = my_pattern.search(filename)
if result and filename.lower().endswith("txt"):
fullname = os.path.join(root, filename)
shutil.copy(fullname, dest_dir)
目錄不存在,則創(chuàng)建:
if not os.path.isdir(dest_dir): os.makedirs(dest_dir)
匹配名稱
import re
pattern = re.compile("1ABC")
pattern.search(var)
相關(guān)文章
Python設(shè)計(jì)模式之外觀模式實(shí)例詳解
這篇文章主要介紹了Python設(shè)計(jì)模式之外觀模式,結(jié)合實(shí)例形式詳細(xì)分析了外觀模式的概念、原理、用法及相關(guān)操作注意事項(xiàng),需要的朋友可以參考下2019-01-01
python關(guān)于os.walk函數(shù)查找windows文件方式
這篇文章主要介紹了python關(guān)于os.walk函數(shù)查找windows文件方式,具有很好的參考價(jià)值,希望對(duì)大家有所幫助,如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-08-08
pandas實(shí)現(xiàn)按照Series分組示例
本文主要介紹了pandas按照Series分組示例,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08
通過實(shí)例簡(jiǎn)單了解Python sys.argv[]使用方法
這篇文章主要介紹了通過實(shí)例簡(jiǎn)單了解Python sys.argv[]使用方法,文中通過示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友可以參考下2020-08-08
關(guān)于阿里云oss獲取sts憑證 app直傳 python的實(shí)例
今天小編就為大家分享一篇關(guān)于阿里云oss獲取sts憑證 app直傳 python的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過來看看吧2019-08-08
python小例子-縮進(jìn)式編碼+算術(shù)運(yùn)算符+定義與賦值
這篇文章主要給大家分享一些python學(xué)習(xí)小例子,內(nèi)容包括縮進(jìn)式編碼風(fēng)格、算術(shù)運(yùn)算符、定義與賦值,需要的小伙伴可以參考一下2022-04-04

