Python os和sys 模块的总结和使用os模块写的一个匹配指定目录下的文件
os 和sys 模块平常使用的不多,这里记录下一些常用的用法,方便用的时候查看。
一、os模块相关:
os.getcwd() # 返回当前工作目录
os.listdir(path) # 返回path指定的文件夹包含的文件或文件夹的名字的列表。
os.remove(path) # 删除路径为path的文件。如果path 是一个文件夹,将抛出OSError;
os.removedirs(path) # 递归删除目录。
os.rename(src, dst) # 重命名文件或目录,从 src 到 dst
os.rmdir(path) # 删除path指定的空目录,如果目录非空,则抛出一个OSError异常
二、os.path模块的常用方法
os.path.abspath(path) # 返回绝对路径
os.path.basename(path) # 返回文件名
os.path.dirname(path) # 返回文件路径
os.path.getsize(path) # 返回文件大小,如果文件不存在就返回错误
os.path.isabs(path) # 判断是否为绝对路径
os.path.isfile(path) # 判断路径是否为文件
os.path.isdir(path) # 判断路径是否为目录
os.path.split(path) # 把路径分割成 dirname 和 basename,返回一个元组
os.path.splitext(path) # 分割路径,返回路径名和文件扩展名的元组
os.path.walk(path, visit, arg) # 遍历path,进入每个目录都调用visit函数,
# visit函数必须有3个参数(arg, dirname, names),
# dirname表示当前目录的目录名,names代表当前目录下的所有文件名,
# args则为walk的第三个参数
for root, dirs, files in os.walk(path):
# print(root) # 当前目录路径
# print(dirs) # dirs 是一个 list ,内容是该文件夹中所有的目录的名字
# print(files) # 当前路径下所有非目录子文件(不包括子目录)
三、sys模块
sys.argv # 实现从程序外部向程序传递参数
sys.exit([arg]) # 程序中间的退出,arg=0为正常退出
sys.path # 获取指定模块搜索路径的字符串集合,可以将写好的模块放在得到的某个路径下,
# 就可以在程序中import时正确找到
sys.argv[0] # 获取当当前脚本的绝对路径
使用os模块和re正则模块写了个获取指定路径下包含给定条件的所有文件的小脚本,下面是详细代码。
#!/usr/bin/python3
# -*- coding: utf-8 -*-
# @File : get_all_file.py
# @Software: PyCharm
"""
获取给定的一个文件件的决定路径 返回这个路径下包含某些特征名字的文件
"""
import os
import re
class FileList(object):
"""
dir_path: 要匹配的文件夹的绝对路径
match_name: 要匹配的条件
match: 匹配的规则 judge_end judge_star judge_contain
"""
def __init__(self, dir_path, match_name=None, match_rule="judge_end"):
self.file_dir = dir_path
self.match_name = match_name
self.match_rule = match_rule
@staticmethod
def judge_end(string='', rule_name=None):
"""
判断是否是以. 结尾的xx文件
:param string:
:param rule_name: 想要获取以xx结尾的文件
:param self:
:return:
"""
patten = '^.*\{}$'.format(rule_name)
return re.match(patten, string)
@staticmethod
def judge_start(string='', rule_name=None):
patten = r'^{}.*$'.format(rule_name)
return re.match(patten, string)
@staticmethod
def judge_contain(string='', rule_name=None):
patten = r'^.*{}.*$'.format(rule_name)
return re.match(patten, string)
def get_files(self):
"""
返回查询到的所有文件的绝对路径的一个list
:return:
"""
file_dirs = []
for root, dirs, files in os.walk(self.file_dir):
# print(root) # 当前目录路径
# print(files) # 当前路径下所有非目录子文件
for f in files:
if self.match_rule == "judge_end":
fun = self.judge_end(f, self.match_name)
elif self.match_rule == "judge_start":
fun = self.judge_start(f, self.match_name)
elif self.match_rule == "judge_contain":
fun = self.judge_contain(f, self.match_name)
else:
raise Exception("match_rule 输入不符合要求")
if fun:
file_dirs.append(os.path.join(root, f))
return file_dirs
def get_file_name_file_path(self):
"""
返回查询到的所有文件的名字和绝对路径的的字典
:return:
"""
file_dict = {}
for root, dirs, files in os.walk(self.file_dir):
for f in files:
if self.match_rule == "judge_end":
fun = self.judge_end(f, self.match_name)
elif self.match_rule == "judge_start":
fun = self.judge_start(f, self.match_name)
elif self.match_rule == "judge_contain":
fun = self.judge_contain(f, self.match_name)
else:
raise Exception("match_rule 输入不符合要求")
if fun:
if file_dict.get(f):
continue
file_dict[f] = os.path.join(root, f)
return file_dict
if __name__ == '__main__':
file_dir = r'D:\work\split_excel_1'
file_list = FileList(file_dir, 'xlsx', match_rule="judge_end").get_file_name_file_path()
print(file_list)
file_dict = FileList(file_dir, 'xlsx', match_rule="judge_end").get_file_name_file_path()
print(file_dict)
老铁,可以哟
速度貌似还行啊
sadrfgtghjkl;
用的是啥统计访问量。看起来是 Set-Cookie 标记不同用户,在后端程序上统计…
评论后这样不太友好: {"code": 200, "success": true, "message": "comment success"}
嗯嗯,是设置的coockie
顶楼上,填邮箱用户体验有点差
给你一个拥抱 评论区能不能不填邮箱
谢谢老铁,邮箱方便接收回复信息的,如果不愿意接收,随便写一个也行的。