跨文件夹移动文件
import osimport sysBASE_DIR = os.path.dirname(os.path.dirname(__file__))sys.path.append(BASE_DIR)def move_file(file, folder): if not (os.path.exists(file) and os.path.isfile(file)): print('文件不存在或非法') return False if not os.path.exists(folder): os.makedirs(folder) file_name = os.path.split(file)[1]
递归删除的思路
def delete_dir(folder): for path in os.listdir(folder):
递归遍历打印目标路径中所有的txt文件
def print_txt(folder): if not os.path.exists(folder) or os.path.isfile(folder): return for path in os.listdir(folder): file_path = os.path.join(folder, path) if os.path.isfile(file_path) and file_path.endswith('.txt'): print(path) elif os.path.isdir(file_path): print_txt(file_path)
项目开放周期
'''1.调研2.需求分析3.架构师完成项目demo,完成项目架构4.分工5.写代码6.白盒黑盒测试7.项目审核发布 => 项目 -> 产品''''''bin: 可执行文件,入口,入口也可以放在项目根目录下core: 核心代码db:数据库相关文件interface:接口lib:包、模块、第三方文件夹log:日志setting:配置static:静态文件'''
# 递归遍历
def list_file(folder, suffix, ls=[]):
if not os.path.exists(folder):
return ls
if os.path.isfile(folder):
if folder.endswith(suffix):
ls.append(folder)
return ls
for file in os.listdir(folder):
file_path = os.path.join(folder, file)
list_file(file_path, suffix)
return ls
folder = r'F:\python8期\课堂内容\day18\代码'
suffix = 'py'
ls = list_file(folder, suffix)
print(ls)
# 递归删除
def delete_folder(folder):
if not os.path.exists(folder):
return False
if os.path.isfile(folder):
os.remove(folder)
return True
for file in os.listdir(folder):
file_path = os.path.join(folder, file)
if os.path.isfile(file_path):
os.remove(file_path) # 子文件删空了
else:
delete_folder(file_path) # 子文件夹删空了
os.rmdir(folder) # 可以删除当前文件夹了
folder = r'F:\python8期\课堂内容\day19\代码\tt'
delete_folder(folder)
# 验证码
def random_code0(num):
code = ""
for i in range(num):
d = random.randint(65, 90)
x = random.randint(97, 122)
n = random.randint(0, 9)
code += random.choice([chr(d), chr(x), str(n)])
return code
def random_code1(num):
code = ""
for i in range(num):
choose = random.randint(1, 3)
if choose == 1:
c = chr(random.randint(65, 90))
elif choose == 2:
c = chr(random.randint(97, 122))
else:
c = str(random.randint(0, 9))
code += c
return code
def random_code2(num):
target = '1234567890qwertyuiopasdfghjklzxcvbnmQWERTYUIOPASDFGHJKLZXCVBNM'
code_list = random.sample(target, num)
return ''.join(code_list)
r1 = random_code2(18)
print(r1)
JSON
pickle
hashlib
hmac
import hmac
shutil
shelve
转载于:https://www.cnblogs.com/haojunliancheng/p/10833867.html
相关资源:Freescale系列单片机常用模块与综合系统设计实例精讲