AI的系统学习之路 1.函数,python文件os的运用

这段代码展示了Python中函数的使用,包括默认参数、可变参数和关键字参数。同时,它涉及到类的方法,如`__call__`,`__copy__`等特殊方法。此外,还包含了文件和目录的操作,如列出目录内容,检查文件类型以及递归遍历文件夹统计特定类型的文件数量。
摘要由CSDN通过智能技术生成

'''def display_name(name, age, tel):
print(f"你好我是{name},今年{age}岁,电话{tel}。")
name = "hl"
age = 20
tel = "123"
display_name(name, age, tel)
def get_score(*score, name):
print(f"{name}")
print(max(score))
print(min(score))
print(sum(score)/len(score))
get_score(12, 21, 3, 4, 14, 6, name = 'hl')'''
'''def changearg(**j):
print(j)
changearg(a=1,b=3)

def mix(*args, **kwargs):
for arg in args:
print(arg)
for key, value in kwargs.items():
print(f"{key}-->{value}")
mix("a", 2, name="ghl", item = "33")

def mix(*args, **kwargs):
for arg in args:
print(arg)
for key, value in kwargs.items():
print(f"{key}-->{value}")
mix("a", 2, name="ghl", item = "33")

a=100
def local(a):
a=200
print(a)
print(a)
print(local(a))
from print import random as r
print(r.randint(1,5))
import math
m=math
sigmoid_a = lambda a,b: a+1/1+m.exp(-b)
print(sigmoid_a(5,6))


'''
'''class A:

def __call__(self):
print("__call__")
def __copy__(self):
print("__copy__")

a=A()
a()

class B:
def __str__(self):
return "hello"
b=B()
print(b)

class L:
def __len__(self):
return 100
l=L()
print(len(l))

class Student:
def __init__(self, name, age, sex, num):
print(f"name: {name}, age: {age}, sex: {sex}, num: {num}")

stu1 = Student(name="ghl", age=20, sex="men", num="123456")
'''

'''class a:
def __new__(cls):
print('__new__')
return object.__new__(cls)

def __init__(self):
print('__init__')

def __del__(self):
print("__del__")

def dis_name(self):
print("name")


a1 = a()
a1.dis_name()

class Utils:
def factorial(n):
if n == 0 or n == 1:
return 1
else:
return n * Utils.factorial(n - 1)

result = Utils.factorial(10)
print(result)

'''
'''
import os
fl = os.listdir(r"d:\\")
print(fl)
for fn in fl:
#判断是文件夹还是文件
if os.path.isdir(f"d:\\{fn}"):
print(f"路径d:\\{fn}")
elif os.path.isfile(f"d:\\{fn}"):
print(f"文件d:\\{fn}")'''

import os
count_txt=0
count_py=0
count_dll=0
count_exe=0
def get_dir(path):
file_lists = os.listdir(path)


for file in file_lists:
file_path=f"{path}//{file}"
if os.path.isdir(file_path):
get_dir(file_path)
else :

if file_path.endswith(".txt"):
#如果是".txt"后缀名文件
print(f"txt文件:{file_path}")
global count_txt
count_txt= count_txt + 1
elif file_path.endswith(".py"):
#如果是".py"后缀名文件
print(f"py文件:{file_path}")
global count_py
count_py = count_py + 1
elif file_path.endswith(".dll"):
#如果是".dll"后缀名文件
print(f"dll文件:{file_path}")
global count_dll
count_dll = count_dll + 1
elif file_path.endswith(".exe"):
#如果是".exe"后缀名文件
print(f"exe文件:{file_path}")
global count_exe
count_exe = count_exe + 1
else:
return


get_dir(r"E:\Wechat开发")
print(f"有{count_txt}个txt文件,有{count_py}个py文件,有{count_dll}个dll文件,有{count_exe}个exe文件")

import os
count_txt = 0
count_py = 0
count_dll = 0
count_exe = 0
def count_files(path):
global count_txt, count_py, count_dll, count_exe
file_lists = os.listdir(path)
for file in file_lists:
file_path=os.path.join(path,file)
if os.path.isdir(file_path):
count_files(file_path)
else:
if file_path.endswith(".txt"):
count_txt += 1
elif file_path.endswith(".py"):
count_py += 1
elif file_path.endswith(".dll"):
count_dll += 1
elif file_path.endswith(".exe"):
count_exe += 1
target_dir = r"E:\Wechat开发"
count_files(target_dir)
print(f"有 {count_txt} 个txt文件,有 {count_py} 个py文件,有 {count_dll} 个dll文件,有 {count_exe} 个exe文件")

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值