| 列表 | ||
| 1 | for 循环语句,打印数字1~10 | 基础 |
| 2 | 如何创建文价夹 | 基础 |
| 3 | 判断文件夹 | 工具 |
| 4 | 打开网页 | 基础 |
| 5 | 文件夹分类工具 | 工具 |
1.循环语句,打印数字1~10
for i in range(0,10):
print(i)
2.创建文件夹
import os
os.makedirs('tmp1/04',exist_ok=True)
备注:tmp1/04 将文件夹建立在当前程序.py文件路径之下
3.判断问价夹中某文件是否存在
path = 'D:/Users/xuyf2/Desktop/project_all/11_practice/python_base'
# os.path.basename(path)
print(os.path.dirname(path))
if (os.path.exists('tmp/02/copy.txt')) == True:
print('存在')
else:
print('不存在')
#直接判断是,输出结果为布尔值,案例如下
# print(os.path.exists('tmp/02/11.txt'))
4.打开网页
# 打开网页
import webbrowser
webbrowser.open('http://www.byhy.net/')
5.文件夹分类工具
#-------------------------文件分类工具(根据名称任意符)------------------------------
import os
import shutil
src_dir_path = 'E:/aeg-paddle/jier_photos/SOCO6045/OK' # 源文件夹
to_dir_path = 'E:/aeg-paddle/jier_photos/SOCO6045/B16' # 存放复制文件的文件夹
key = '_B16_' # 源文件夹中的文件包含字符key则复制到to_dir_path文件夹中
if not os.path.exists(to_dir_path):
print("to_dir_path not exist,so create the dir")
os.mkdir(to_dir_path, 1)
if os.path.exists(src_dir_path):
print("src_dir_path exist")
for file in os.listdir(src_dir_path):
# is file
if os.path.isfile(src_dir_path + '/' + file):
if key in file:
print('找到包含"' + key + '"字符的文件,绝对路径为----->' + src_dir_path + '/' + file)
print('移动到----->' + to_dir_path + file)
shutil.move(src_dir_path + '/' + file, to_dir_path + '/' + file) # 移动用move函数
持续更新中......



