1,os模块

os模块获取电脑的相关信息并且有很强大的文件及文件夹操作能力,所以在操作文件或者文件夹的时候首先要引入os模块

关于os的一些代码  简单的几个例子

import os
# 获取电脑cpu个数
cpuCount = os.cpu_count()
print(cpuCount)
name = os.name
# nt代表windows操作系统 linux为posix
print('操作系统的名字是:{}'.format(name))

path路径

# exists存在 path路径
# 相对路径
result = os.path.exists('1.homework.py')
if result :print('存在')
else :print('不存在')print(result)# C:\Users\a\Desktop\os测试
# C:/Users/a/Desktop/os测试
result = os.path.exists('C:/Users/a/Desktop/os测试/python.txt')
print(result)result = os.path.abspath('周二.py')
print(result)

path路径的相关操作

#获取文件路劲的某一部分
result = os.path.basename('C:/Users/a/Desktop/os测试')
print('路径的basename:{}'.format(result))result = os .path.commonpath(['C:/Users/a/Desktop/os测试','C:/Users/a/Desktop/同屏,''C:/Users/a/Desktop/文件夹集合'])
print('路径的公共部分为:{}'.format(result))
#  注意: 以/分割  将路径分成几部分  找到公共的这一部分
result  = os.path.commonpath(['http://www.baidu.com','http://www,jd.com,','http://www.taobao.com'])
print('网址的公共部分为:{}'.format(result))
#  directory name 获取指定文件所在的文件夹路径
result = os.path.dirname('C:/Users/a/Desktop/os测试/pytion.txt')
print(result)

2,文件夹相关操作

包括,创建,修改,访问日期  增删改查等功能!

import  time
result = os.path.getctime('C:/Users/Administrator/Desktop/pytion')
print('文件创建日期为 :{}'.format(time.localtime(result)))#a : access  访问
result = os.path.getatime('C:/Users/Administrator/Desktop/pytion')
print('文件访问的日期是:{}'.format(time.localtime(result)))result = os.path.getatime('C:/Users/Administrator/Desktop/pytion')
print('文件的修改日期是:{}'.format(time.localtime(result)))result = os.path.getsize('C:/Users/Administrator/Desktop/pytion')
#获取的大小为字节大小   kb
print('文件的大小为:{}'.format(result/1024))# isfile  判断是否为文件
result = os.path.isfile('C:/Users/Administrator/Desktop/pytion')
print('{}'.format(result))#文件分割result = os.path.split('C:/Users/Administrator/Desktop/os测试/python.txt')
print('{}'.format(result))
#文件后缀
result = os.path.splitext('C:/Users/Administrator/Desktop/os测试/python.txt')
print('{}'.format(result))#文件夹增删改操作  值一 原名   值二  后值
# os.rename('周二.txt','皮皮猪.txt')
if os._exists('皮皮猪.txt'):os.remove('皮皮猪.txt')   #删除文件  removedirs  删除文件夹# os.mkdir('test')

关于文件添加的小程序

# 小程序
# 1随意输入一个输入  如果是1  创建一个文件夹名字为text_one
# 2如果是2  删除一个文件夹 名字是  test_one
# 3如果是其他数字 返回
# whlie后面跟一个条件  条件为真的情况下  会一直执行  知道条件为假
# x = int(input('请输入一个数:'))
# if x==1:
#        os.mkdir('test_one')
# elif x == 2:
#        os.removedirs('test_one')
# else:
#        pass

关于获取文件路径

print('{}'.format(os.path.abspath('.')))
print('{}'.format(os.getcwd()))
#change 改变 改变当前所在的目录
os.chdir('test')
#获取路径  获取当前路径的父路径
os.path.abspath('..')
#改变路径到指定的路径下  pardir  directory parent
os.chdir(os.path.pardir)
print('{}'.format(os.getcwd()))
# os.removedirs('testA')

文件读写

# open 打开指定的文件  如果文件不存在 则创建
# w = write
f = open('os.txt','w',encoding='utf-8')
f.write('hello word\n')
f.write('你好\n')
f.writelines(['张三\n','李四\n','王五\n'])
f.close()
#当文件关闭后  不能再继续对这个文件进行操作   否则会报错

文件内容追加

f = open('new.txt','w',encoding='utf-8')
f.write('人生三大难,早上是啥,中午吃啥,晚上吃啥\n')
f.close()
#a append 追加 ,添加
f= open('new.txt','a',encoding='utf-8')
f.write('谁都不想吃')
f.close()

3,练习题

创建一个文件  名字为code.txt  在里面存放10000个随机数字的验证码
import random
f = open('code.txt','w',encoding='utf-8')
for x in range(0,10000):num = random.randint(0,999999)num = '%.6d' % numf.write(num + '\n')
f.close()
两种方法
f = open('code1.txt','w',encoding='utf-8')
for x in range(0,10000):content = ''for y in range(6):num = random.randint(0,9)content +=str(num)f.write(content + '\n')
f.close()
2小练习 ;创建一个文件,在里面存放100000个10随机验证码(3种方法)
# 例如:dadd454545
1.
import random
str = 'qwertyuiopasdfghjkjlmnnbvcxz123456789QWERTYUIOPASDFGHJKLZXCVBNM'
# f = open('ppap.txt','w',encoding='utf-8')
with open('random.txt','w',encoding='utf-8')as f :for index in range(10000):content = ''for x in range(10):char_str = random.choice(str)  #如果对象是一个列表,元祖或者字符串的时候   从这个对象中随机取出一块content +=char_strf.write(content + '\n')+
f.close()
2.
with open('random.txt','w',encoding='utf-8')as f :for index in range(10000):content = ''for x in range(10):char_str = str[random.randint(0,len(str)-1)]content += char_strf.write(content + '\n')
f.close()
3.for x in range(0,10000):content = ''for y in range(10):n = random.randint(0,2)if n == 0:num = random.randint(0,9)content +=str(num)else:nnm = random.randint(97,122)content +=chr(nnm)f.write(content + '\n')
f.close()
所有验证码中每一个数字的出现的总次数
data = {}
for x in range(10):data[str(x)] = 0
print(data)
with open('ppap.txt','r',encoding='utf-8')as f :for line in f.readlines():for char in line :for key in data:if char == key :data[key] +=1
print(data)

今天又是元气满满的一天 !   明天继续加油!!!!!!!!!!!!!!

pytion基础之OS和文件操作相关推荐

  1. Python学习小组课程P2-Python基础(2)文件操作

    一.前言 注意:此为内部小组学习资料,非售卖品,仅供学习参考. 本系列课程: Python学习小组课程-课程大纲与Python开发环境安装 Python学习小组课程P1-Python基础(1)语法与数 ...

  2. Python os模块文件操作(二)

    Python os模块文件操作(二) os模块对文件夹和文件的操作很多.可以先看: https://blog.csdn.net/weixin_43790276/article/details/9867 ...

  3. Python os模块文件操作(一)

    Python os模块文件操作(一) 一.文件描述符 在使用代码对文件进行操作时,为了指定操作的文件,避免不了要使用文件描述符,所以我们先介绍什么是文件描述符. 操作系统为了高效管理已经被打开的文件, ...

  4. 2021-09-08 python基础知识学习:文件操作和os模块

    文章目录 1.文件操作(IO技术) (1)打开文件 (2)编码 (3)close()关闭文件流 (4)文本文件读取 (5)二进制文件的读写 (6)文件对象常用的方法和属性 (7)使用pickle序列化 ...

  5. 享学课堂python基础学习day15之文件操作

    我是一个22年8月加入享学课堂的学生,主要学习python基础和爬虫课程,学到现在,发现以前的学习知识容易忘记,听了老师的建议之后,决定在CSDN写博客,可以复习自己学过的知识,废话不多说,文件操作里 ...

  6. Python基础:集合与文件操作

    集合 集合是无序的,不重复的数据集合,它里面的元素是可哈希的(不可变类型),但是集合本身是不可哈希(所以集合做不了字典的键)的.以下是集合最重要的两点: 去重,把一个列表变成集合,就自动去重了. 关系 ...

  7. 01语言基础-系统模块和文件操作20220428

    DAY14系统模块和文件操作 01time模块 from time impot * 1.1 time() - 获取当前时间的时间戳 1.2 localtime()-获取本地时间,返回值是结构体时间 ​ ...

  8. Python基础(十三)——文件操作(open函数、close函数)

    本文以Python3以上为学习基础. 目录 1. 使用文件操作第一原则 2.open函数 2.1.文件打开模式 2.1.1.只读模式打开文件--只读(r) 2.1.2.读写模式打开文件--读写模式(r ...

  9. Python基础(三)文件操作和处理json

    文件操作步骤:1.有一个文件,2.打开文件,3.读写修改文件,4.关闭文件 一.有一个文件:新建或导入文件 二.打开文件:如果是新建的文件默认和py文件在同一个目录:如果是打开文件,要将文件放在py同 ...

最新文章

  1. 【竞赛总结】新冠期间饿了么骑士行为预估
  2. 数据库02_字段类型
  3. Exadata上oracle binary的make日志
  4. *【HDU - 1242 】 Rescue (反向dfs,或bfs)
  5. 描述最常用的5种http方法的用途_RESTful API系列之HTTP基础
  6. 2 追踪光线=》2.6 反射光线
  7. 编程时遇到问题的解决方向
  8. 联想Win7 SP1 32位/64位OEM系统[官方原版]
  9. Python导入模块,Python import用法(超级详细)
  10. 【程序源代码】番茄时间小程序
  11. 验证码功能-简单实现
  12. android 取消root,彻底告别安卓刷机时代!360超级ROOT正式宣布下线:取消ROOT权限
  13. Xcode运行报错The operation couldn’t be completed.
  14. High-Resolution Net(HRNet) 论文笔记
  15. 300etf期权怎么玩?正规平台有哪些呢?
  16. micropython STM32移植笔记(一)
  17. 【终极之战】基于Vue3+Vant3造一个网页版的类掘金app项目 - 个人主页
  18. 微信小程序实现图片预览功能
  19. 给div加滚动条 div显示滚动条设置代码
  20. mysql dwith boost_【云知梦】CentOS8.2上如何编译安装MySQL8?

热门文章

  1. cfiledialog 保存 扩展名_开春换季鞋子的保存大法,快快收下!有备无患!
  2. maven-shade-plugin A required class was missing org/sonatype/aether/version/VersionConstraint
  3. hdu - 2851 Lode Runner (最短路)
  4. java useragent 解析_java_java 解析user-agent 信息,解析http user-agent信息,使用uasp - phpStudy...
  5. H5导出HTML文件,H5 导出文件的解决办法
  6. matlab计算复活节概率,复活节的算法_复活节日期怎么算?
  7. win10控制面板快捷键_Win10系统输入法只能输入英文的解决办法
  8. 鼠标移入显示移出隐藏及反复闪烁问题
  9. thinkphp5使用redis实现秒杀商品活动
  10. 淘宝 Api 查询手机号