#!/usr/bin/env python
# -*- coding:utf-8 -*-
# os=OperationSystem 操作系统(可以操作文件/文件夹/路径)
import os
# 获取cpu的个数,个数越大运行速度越快
# 根据cpu的个数可以决定创建几个进程更合适,cpu的个数=最好开启的进程数
count = os.cpu_count()
print("cpu的个数为{}".format(count))
# 返回当前文件所在文件夹的绝对路径
cwd = os.getcwd()
print("当前项目的绝对路径是{}".format(cwd))
# nt:Windows
# 返回当前的操作系统名称
name = os.name
print("操作系统名称是:{}".format(name))
# ========================下面的这些没啥用
# 获取当前平台的行终止符 windows使用\r\n Mac使用\r Linux使用\n
linesep = os.linesep
print("{}".format(linesep))
# stat = state状态
state = os.stat("C:/")
print("{}".format(state))
# mk=make创建/制造 有问题
# os.mknod("empty.txt")
# ====================================================
# os和路径
# 路径path
# C:/Users/Administrator/Desktop/2018_3_22/day1.py
# C:\\Users\\Administrator\\Desktop\\2018_3_22\day1.py
# 相对路径:用的比较多, 2018_3_22/day1.py,相对于桌面
# 绝对路径:上传文件,特点:路径很完整
# abs=absolute绝对
# 返回该路径对应的绝对路径
result = os.path.abspath("1.py")
print("绝对路径是:{}".format(result))
# 取路径的最后一个部分
# 网址也属于路径的一种,例如 http://www.baidu.com/1.jpg
result = os.path.basename("C:/Users/Administrator/Desktop/2018_3_22")
print("路径的最后一部分是:{}".format(result))
# common:共有的,通用的
result = os.path.commonpath(["c:/a/b", "c:/a/c", "c:/a/b/c/d"])
print("路径的公共部分是:{}".format(result))
# dir=directory文件夹
# 返回当前文件/文件夹 所在文件夹的路径
result = os.path.dirname("C:/Users/Administrator/Desktop/2018_3_22/1.py")
print("上级目录的路径为:{}".format(result))
# exists存在
# 测试路径下的文件/文件夹是否存在 True存在 False不存在
# 创建文件/文件夹 删除文件/文件夹
result = os.path.exists("C:/Users/Administrator/Desktop/2018_3_22/1.html")
print("路径对应的文件/文件夹是否存在:{}".format(result))
import time
# 获得时间有助于删除老/不用的文件
# atime:AccessTime访问时间
result = os.path.getatime("C:/Users/Administrator/Desktop/2018_3_22/1.html")
print("{}".format(time.localtime(result)))
# ctime:ChangeTime修改时间
result = os.path.getctime("C:/Users/Administrator/Desktop/2018_3_22/1.html")
print("{}".format(time.localtime(result)))
# mtime:最后一次修改时间
result = os.path.getmtime("C:/Users/Administrator/Desktop/2018_3_22/1.html")
print("{}".format(time.localtime(result)))
# 获取路径对应的文件大小,单位是字节B KB千字节
file_size = os.path.getsize("C:/Users/Administrator/Desktop/2018_3_22/1.html")
print("文件大小是:{}".format(file_size/1024))
# 是否为绝对路径
# os.path.isabs()
# 是否为文件 True是文件 False不是文件
result = os.path.isfile("C:/Users/Administrator/Desktop/2018_3_22")
print(result)
# 是否为文件夹
# os.path.isdir()
# 是否为超链接
# os.path.islink()
# split:分割 "字符串".split()
# 返回一个元组,
result = os.path.split("C:/Users/Administrator/Desktop/2018_3_22/1.html")
print(result)
# 返回一个元组,包含一个文件后缀
result = os.path.splitext("C:/Users/Administrator/Desktop/2018_3_22/1.html")
print(result)
# ============================================================================
# 文件操作 / 文件夹操作
# 文件的写入
# 文件的读写模式: w=write写入 r=read读 a=append追加 b=bytes二进制形式 +读写模式
# w:每次都会先清空文件再写入
# open:写入文件时,若文件不存在则创建一个新文件
# close:文件关闭时会立即把数据写入文件, 否则在程序结束时再写入
# writelines:不会自动换行,需要手动
f = open("1.txt", "w", encoding="utf-8")
f.write("hello world\n")
f.writelines(['张三\n', '李四\n', '王五\n'])
# f.flush() # 刷新文件内部缓冲,直接把内部缓冲区的数据立刻写入文件, 而不是被动的等待输出缓冲区写入。
# time.sleep(5)
f.close()
# import random
# f = open("num.txt", "w", encoding="utf-8")
# # 练习:创建一个文件 num.txt, 写入10000个6位随机数,每个数字单独一行
# for num in range(10000):
# random_6_num = random.randint(100000, 999999)
# random_6_num = str(random_6_num)
# f.write(random_6_num+"\n")
# f.close()
# 读文件
# read:默认读取所有, 小文件可以用,大文件会占据大量内存
# read(大小):读取指定大小的内容,可以将大文件分段读取更省内存
f = open("num.txt", "r", encoding="utf-8")
# content = f.read()
# print(content) # 字符串类型
# content = f.read(20)
# print(content) # 字符串类型
# 读取数据的之后文件指针会自动后移
# content = f.readline()
# content = f.readline()
# print(content) # 字符串类型
content = f.readlines()
print(content) # 列表类型,通过索引/切片操作很方便
f.close()
# 通过遍历文件句柄读文件
# 内部是通过next来实现遍历的
f = open("num.txt", "r", encoding="utf-8")
for line in f:
print(line.strip())
f.close()
# 文件读写的推荐写法
with open("num.txt", "r", encoding="utf-8") as f:
# f.read()
"自己的代码"
# 写出来
# 1) 牛逼 时间慢
# 2) 不知道咋写 百度 时间快
# 没写出来
# 1) 没思路
# 2) 没思路百度 看不懂
# 综合练习
# 1. 写入一个文件, 包含10000个10位随机字符串的验证码,一个一行
# 例如 ad5sZ89RgY
import random
random_str = "qwertyuioplkjhgfdsazxcvbnmQWERTYUIOPLKJHGFDSAZXCVBNM0123456789"
with open("a.txt", "w", encoding="utf-8") as f:
for x in range(10000):
result = ""
for i in range(10):
choice_str = random.choice(random_str)
result += choice_str
f.write(result+'\n')
# 2. 读入上一个文件,统计验证码中数字个数分别位1,2,3,4...10次的验证码信息并打印
word_dict = {
"0": [], "1": [], "2": [], "3": [], "4": [],
"5": [], "6": [], "7": [], "8": [], "9": [], "10": [],
}
# from prettyprinter import pprint as print
with open("a.txt", "r", encoding="utf-8") as f:
for line in f:
count = 0
for word in line:
if word.isdigit():
count += 1
word_dict[str(count)].append(line)
print(word_dict)
for key, value in word_dict.items():
print("数字个数为{}有{}个,分别是{}".format(key, len(value), list(map(str.strip, value))))
# ===================================================================================
# 递归文件夹创建函数。像mkdir(), 但创建的所有intermediate-level文件夹需要包含子文件夹。
os.makedirs("a/b/c/d", exist_ok=True)
# 以数字mode的mode创建一个名为path的文件夹.默认的 mode 是 0777 (八进制)。
# 只能创建一级,再次创建会报错
# os.mkdir("d")
# 删除路径为path的文件。如果path 是一个文件夹,将抛出OSError; 查看下面的rmdir()删除一个 directory。
# os.remove("a/b/c/d/a.py")
# 递归删除目录。
os.removedirs("a/b/c/d")
# 重命名文件或目录,从 src 到 dst
os.rename("1.html", "2.html")
# 递归地对目录进行更名,也可以对文件进行更名。
# os.renames(old, new)
# 删除path指定的空目录,如果目录非空,则抛出一个OSError异常。
# os.rmdir(path)
import os
import os.path
"""
获取指定目录及其子目录下的 py 文件路径说明:
l 用于存储找到的 py 文件路径 get_py 函数,递归查找并存储 py 文件路径于 l
"""
l = []
def get_py(path,l):
fileList = os.listdir(path) #获取path目录下所有文件
for filename in fileList:
pathTmp = os.path.join(path, filename) #获取path与filename组合后的路径
if os.path.isdir(pathTmp): #如果是目录
get_py(pathTmp, l) #则递归查找
elif filename[-4:].upper() == '.jpg': #不是目录,则比较后缀名
l.append(pathTmp)
path = input('请输入路径:').strip()
get_py(path, l)
print('在%s目录及其子目录下找到%d个py文件\n分别为:\n' % (path, len(l)))
for filepath in l:
print(filepath+'\n')
import os
def search_file(start_dir, target):
os.chdir(start_dir)
for each_file in os.listdir(os.curdir):
ext = os.path.splitext(each_file)[1]
if ext in target:
vedio_list.append(os.getcwd() + os.sep + each_file + os.linesep)
if os.path.isdir(each_file):
search_file(each_file, target) # 递归调用
os.chdir(os.pardir) # 递归调用后切记返回上一层目录
start_dir = input('请输入待查找的初始目录:')
program_dir = os.getcwd()
target = ['.mp4', '.avi', '.rmvb']
vedio_list = []
search_file(start_dir, target)
f = open(program_dir + os.sep + 'vedioList.txt', 'w')
f.writelines(vedio_list)
f.close()
#!/usr/bin/env python
# -*- coding:utf-8 -*-
# 文件读写模式
# w 以写方式打开
# a 以追加模式打开,必要时创建新文件
# r 以读模式打开,文件必须存在
# r+ 以读写模式打开
# w+ 以读写模式打开,参见w
# a+ 以读写模式打开,参见a
# ab 以二进制追加模式打开,参见a
# rb 以二进制读模式打开
# wb 以二进制写模式打开,参见w
# rb+ 以二进制读写模式打开,参见r+
# wb+ 以二进制读写模式打开,参见ab+
# ab+ 以二进制读写模式打开,参见a+
# 打开文件准备读, 若不存在则会新建再打开
f = open("a.txt", "w", encoding="utf-8")
print("文件名是:{}".format(f.name))
f.write("hello world")
# 关闭文件。关闭后文件不能再进行读写操作。
f.close()
import time
f = open("c.txt", "r", encoding="utf-8")
f.seek(100)
content = f.read(10)
f.close()
print(content)
# 从文件的首行首字符开始截断,截断文件为 size 个字符,无 size 表示从当前位置截断;截断之后后面的所有字符被删除,其中 Widnows 系统下的换行代表2个字符大小。
fo = open("d.txt", "r+")
print("文件名: ", fo.name)
line = fo.readline()
print("读取行: %s" % (line))
fo.truncate()
line = fo.readlines()
print("读取行: %s" % (line))
# 关闭文件
fo.close()
with
with open("文件名", "文件模式", encoding="文件编码") as f:
"""
文件相关操作
"""
与直接open相比,好处在于不用关闭.
atime,mtime,ctime
在windows下一个文件有三种时间属性:
- 创建时间
- 修改时间
- 访问时间
inux下一个文件也有三种时间属性
- 访问时间(access time 简写为atime)
- 修改时间(modify time 简写为mtime)
- 状态修改时间(change time 简写为ctime)
关于Linux底下三种时间的简单介绍:
atime:(access time)显示的是文件中的数据最后被访问的时间,比如系统的进程直接使用或通过一些命令和脚本间接使用。(执行一些可执行文件或脚本)
mtime: (modify time)显示的是文件内容被修改的最后时间,比如用vi编辑时就会被改变。(也就是Block的内容)
ctime: (change time)显示的是文件的权限、拥有者、所属的组、链接数发生改变时的时间。当然当内容改变时也会随之改变(即inode内容发生改变和Block内容发生改变时)
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import shelve
# 目的:将一个字典放入文本
# 写数据
f = shelve.open(r"shelve")
f['stu1'] = {"name": "张三", "age": 40}
f.close()
# 读数据
f = shelve.open(r"shelve")
name = f['stu1']['name']
print(name)
f.close()
<?xml version="1.0" encoding="UTF-8"?>
<students>
<student no="2009081097">
<name>Hongten</name>
<gender>M</gender>
<age>20</age>
<score subject="math">97</score>
<score subject="chinese">90</score>
</student>
<student no="2009081098">
<name>DuDu</name>
<gender>W</gender>
<age>21</age>
<score subject="math">87</score>
<score subject="chinese">96</score>
</student>
<student no="2009081099">
<name>Sum</name>
<gender>M</gender>
<age>19</age>
<score subject="math">64</score>
<score subject="chinese">98</score>
</student>
</students>
#!/usr/bin/env python
# -*- coding:utf-8 -*-
import xml.etree.ElementTree as ET
tree = ET.parse("1.xml")
root = tree.getroot()
for student in tree.findall("student"):
name = student.find("name").text
gender = student.find("gender").text
age = student.find("age").text
print("{}--{}--{}".format(name, gender, age))